Updating and deleting data from the shell

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.

Getting ready

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.

How to do it…

  1. With the shell started and the script loaded, execute the following command in the shell:
    > prepareTestData()
    

    If all goes well, you should see 20 documents inserted in updAndDelTest and printed on the console.

  2. To get a feel of the collection, let's query it as follows:
    > db.updAndDelTest.find({}, {_id:0})
    
  3. We will see that for each value of x as 1 and 2, we have y incrementing from 1 to 10.
  4. We will first update some documents and observe the results. Execute the following update command:
    > db.updAndDelTest.update({x:1}, {$set:{y:0}})
    
  5. Execute the following 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})
    
  6. We will now execute the following update command:
    > db.updAndDelTest.update({x:1}, {$set:{y:0}}, false, true)
    
  7. Executing the query given in step 5 again to view the updated documents will show the same documents we saw earlier. Take a note of the values of 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.
  8. We will now see how the 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})
    
  9. Execute the following 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})
    

How it works…

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.

Note

The default behavior of update and remove is different. An update call by default updates only the first matching document, whereas remove deletes all the documents that match the query.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset