Saturday 29 April 2017

Confusion regarding Mongo db's CRUD statements

I started learning Mongo db today and am little confused about following update statement.

var collection = db.collection('test');
var doc = {mykey:1, fieldtoupdate:1};

collection.insert(doc, {w:1}, function(err, result) {
collection.update({mykey:1}, {$set:{fieldtoupdate:2}}, {w:1}, function(err, result) {});
});

My specific questions:

  1. Why are we calling update inside insert? Isn't insert supposed to be used for inserting a new doc in the collection?
  2. Can't I simply write following instead of writing it in the callback of insert?

    var collection = db.collection('test');
    var doc = {mykey:1, fieldtoupdate:1};
    collection.update({mykey:1}, {$set:{fieldtoupdate:2}}, {w:1}, function(err, result) {});
    
    


via nitinsh99

No comments:

Post a Comment