angularjs - How to update array elements in mongodb -


i have data in mongodb below

{   "_id": objectid("57c40e405e62b1f02c445ccc"),   "sharedpersonid": objectid("570dec75cf30bf4c09679deb"),   "notificationdetails": [      {        "userid": "57443657ee5b5ccc30c4e6f8",        "username": "kevin",        "isred": true       }     ]  } 

now want update isred value false how write query this. have tried below

var updateisredfield = function(db, callback) {     db.collection('notifications').update({_id:notificationid},     {         $set: { "notificationdetails.$": {isred: false}},     }, function(err, results) {         callback();     }); };  mongoclient.connect(config.database, function(err, db) {     assert.equal(null, err);     updateisredfield(db, function() {         db.close();     });     return res.json({}); }); 

since positional $ operator acts placeholder first element matches query document, , array field must appear part of query document hence query { "notificationdetails.isred": true } essential $ operator work properly:

db.collection('notifications').update(     { "_id": notificationid, "notificationdetails.isred": true },     { "$set": { "notificationdetails.$.isred": false } },     function(err, results) {         callback();     } ); 

Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -