This again will be a simple recipe that will be looking at executing deletes and updates on a test collection. We won't be dealing with the same test data that we imported as we don't want to update/delete any of that, but instead, we will work on a test collection created for this recipe only.
For this recipe, we will create a collection called updAndDelTest
. We will require the server to be up and running. Refer to the Installing single node MongoDB recipe from Chapter 1, Installing and Starting the Server for instructions on how to start the server. Start the shell with the UpdAndDelTest.js
script loaded. This script will be available on the Packt website for download. To know how to start the shell with a script preloaded, refer to the Connecting to a single node in the Mongo shell with JavaScript recipe in Chapter 1, Installing and Starting the Server.
$ mongo --shell updAndDelTest.js
> prepareTestData()
Inserted 20 documents in updAndDelTest
printed to the console:> db.updAndDelTest.find({}, {_id:0})
x
as 1
and 2
, we have y
incrementing from 1 to 10 for each value of x
.> db.updAndDelTest.update({x:1}, {$set:{y:0}})
find
command and observe the results; we should get 10 documents. For each of them, note the value of y
.> db.updAndDelTest.find({x:1}, {_id:0})
> db.updAndDelTest.update({x:1}, {$set:{y:0}}, {multi:true})
y
again and compare them to the results that we saw when we executed this query last time before executing the update given in step 7.x
is 1
for the deletion test. Let's delete all the documents where x
is 1
from the collection:> db.updAndDelTest.remove({x:1})
find
command and observe the results. We will not get any results. It seems that the remove
operation has removed all the documents with x
as 1
.> db.updAndDelTest.find({x:1}, {_id:0})
When you are in the mongo shell and you want to see the source code of a function, simply type in the function name without the parenthesis. For example, in this recipe, we can view the code of our custom function by typing the function name, prepareTestData
, without the parenthesis, and press Enter.
First, we set up the data that we will use for the updating and deleting test
. We have already seen the data and know what it is. An interesting thing to observe is that when we execute an update such as db.updAndDelTest.update({x:1}, {$set:{y:0}})
, it only updates the first document that matches the query provided as the first parameter. This is something we will observe when we query the collection after this update. The update function has the following format db.<collection name>.update(query, update object, {upsert: <boolean>, multi:<boolean>})
.
We will see what upsert is in the later recipes. The multi parameter is set to false
by default. This means that multiple documents will not be updated by the update
method; only the first matching document will be updated. However, when we do db.updAndDelTest.update({x:1}, {$set:{y:0}}, {multi:true})
with multi set to true
, all the documents in the collection that match the given query are updated. This is something that we can verify after querying the collection.
Removals, on the other hand, behave differently. By default, the remove
operation deletes all the documents that match the provided query. However, if we want to delete only one document, we explicitly pass the second parameter as true
.