Updating Documents in a Collection

Once objects have been inserted into a collection, you may need to update them from time to time as the data changes. The MongoDB Node.js driver provides several great methods for updating documents. The most commonly used is the update() method, which is versatile and yet fairly easy to implement. The following is the syntax for the update() method:

update(query, update, [options], [callback])

The query parameter is a document that is used to identify which document(s) you want to change. The request matches the properties and values in the query parameter with the fields and values of the object, and only those that match the query are updated. The update parameter is an object that specifies the changes to make to the documents that match the query. Table 14.2 lists the operators that can be used.

The options parameter specifies the database change options described in Table 14.1. The callback parameter is required if you are implementing a write concern in options. The first parameter of callback is an error, and the second parameter is an array of the documents inserted into the collection.

When you update multiple documents with the update() call, you can isolate writes to protect the documents from other writes by using the $isolate:1 property in the query. This doesn’t provide an all-or-nothing atomic write but simply inhibits other write processes from updating the same objects you are writing to. For example:

update({type:"Planetary", $isolated:1}, {updated:true}, {multi:true})

Listing 14.3 shows how to update multiple objects by using the update() method. Lines 9–19 implement the update() method and callback to change the type planetary to Planetary and add a new field named updated. Notice that the $set operator is used to set values. Also notice that upsert is false so that new documents will not be created, multi is true so that multiple documents will get updated, and w is 1 so that the request will wait for the write operation before returning (see Figure 14.3).


Note

To run the code in this exercise, please run the code in exercise 14.1 first to reset the data that may have changed by running other exercises in this chapter.


Listing 14.3 doc_update.js: Updating multiple documents in a database


01 var MongoClient = require('mongodb').MongoClient;
02 MongoClient.connect("mongodb://localhost/", function(err, db) {
03   var myDB = db.db("astro");
04   myDB.collection("nebulae", function(err, nebulae){
05     nebulae.find({type:"planetary"}, function(err, items){
06       items.toArray(function(err, itemArr){
07         console.log("Before Update: ");
08         console.log(itemArr);
09         nebulae.update({type:"planetary", $isolated:1},
10                        {$set:{type:"Planetary", updated:true}},
11                        {upsert:false, multi:true, w:1},
12                        function(err, results){
13           nebulae.find({type:"Planetary"}, function(err, items){
14             items.toArray(function(err, itemArr){
15               console.log("After Update: ");
16               console.log(itemArr);
17               db.close();
18             });
19           });
20         });
21       });
22     });
23   });
24 });


Image

Figure 14.3 Updating multiple documents in a database.

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

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