Angularjs 如何在mongodb中更新数组元素

Angularjs 如何在mongodb中更新数组元素,angularjs,node.js,mongodb,Angularjs,Node.js,Mongodb,我在mongoDB中有如下数据 { "_id": ObjectId("57c40e405e62b1f02c445ccc"), "sharedPersonId": ObjectId("570dec75cf30bf4c09679deb"), "notificationDetails": [ { "userId": "57443657ee5b5ccc30c4e6f8", "userName": "Kevin", "isRed": true

我在mongoDB中有如下数据

{
  "_id": ObjectId("57c40e405e62b1f02c445ccc"),
  "sharedPersonId": ObjectId("570dec75cf30bf4c09679deb"),
  "notificationDetails": [
     {
       "userId": "57443657ee5b5ccc30c4e6f8",
       "userName": "Kevin",
       "isRed": true 
     } 
   ] 
}
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({});
});
现在我想将
isRed
值更新为
false
如何为此编写查询。我试过下面的方法

{
  "_id": ObjectId("57c40e405e62b1f02c445ccc"),
  "sharedPersonId": ObjectId("570dec75cf30bf4c09679deb"),
  "notificationDetails": [
     {
       "userId": "57443657ee5b5ccc30c4e6f8",
       "userName": "Kevin",
       "isRed": true 
     } 
   ] 
}
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({});
});

由于位置运算符充当与查询文档匹配的第一个元素的占位符,并且数组字段必须作为查询文档的一部分出现,因此查询
{“notificationDetails.isRed”:true}
对于使运算符正常工作至关重要:

db.collection('notifications').update(
    { "_id": notificationId, "notificationDetails.isRed": true },
    { "$set": { "notificationDetails.$.isRed": false } },
    function(err, results) {
        callback();
    }
);

更新时您有任何错误吗?或者它没有更新?它没有更新。您可以检查输出中的
db.collection('notifications')。查找({u id:notificationId})
?它是否给出任何记录?我之所以得到结果,是因为上次它将整个
通知详细信息
数组更新为
isRed:false