This, again, will be a simple recipe that will look at executing deletes and updates on a test collection. We won't deal with the same test data we imported, as we don't want to update/delete any of this; instead, we will work on a test collection created only for this recipe.
For this recipe, we will create a collection called updAndDelTest
. We will need the server to be up and running. Refer to the Single node installation of MongoDB recipe in Chapter 1, Installing and Starting the MongoDB Server, to learn how to start the server. Also, start the shell with the UpdAndDelTest.js
script loaded. This script will be available on the book's website for download. To know how to start the shell with a reloaded script, refer to the Connecting to a single node from the Mongo shell with a preloaded JavaScript recipe in Chapter 1, Installing and Starting the MongoDB Server.
> prepareTestData()
If all goes well, you should see 20 documents inserted in updAndDelTest
and printed on the console.
> db.updAndDelTest.find({}, {_id:0})
x
as 1
and 2
, we have y
incrementing from 1 to 10.update
command:> db.updAndDelTest.update({x:1}, {$set:{y:0}})
find
command and observe the results; we will get 10 documents (for each of them, note the value of y
):> db.updAndDelTest.find({x:1}, {_id:0})
update
command:> db.updAndDelTest.update({x:1}, {$set:{y:0}}, false, true)
y
again and compare them to the results we saw when we executed this query the last time around before executing the update
command given in step 6.delete
operation works. We will again choose the documents where 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})
First, we set up the data that we would be using for updates and deletion. 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 db.<collection name>.update(query, update object, isUpsert, isMulti)
format.
We will see what Upsert is in Atomic find and modify operations recipe in Chapter 5, Advanced Operations. The fourth parameter (isMulti) is by default false, and this means that multiple documents will not be updated by the update call. So, only the first matching document will be updated by default. However, when we execute db.updAndDelTest.update({x:1}, {$set:{y:0}}, false, true)
with the fourth parameter set to true, all the documents in the collection that match the given query get updated. This is something we can verify after querying the collection.
Removals, on other hand, behave differently. By default, the remove
operation deletes all the documents that match the provided query. However, if we wish to delete only one document, we will explicitly pass the second parameter as true
.