MongoDB 如何更新数组里面的一组对象的属性?

MongoDB 如何更新数组里面的一组对象的属性?

dbDao 百度贴吧:http://tieba.baidu.com/dbdao

MongoDB技术学习QQ群: 421431253

 

如下面的例子

语法如下:

db.collection.update(
   {  },
   { : { "array.$.field" : value } }
)




db.students.insert ({
  _id: 4,
  grades: [
     { grade: 80, mean: 75, std: 8 },
     { grade: 85, mean: 90, std: 5 },
     { grade: 90, mean: 85, std: 3 }
  ]
})


db.students.update(
   { _id: 4, "grades.grade": 85 },
   { $set: { "grades.$.std" : 6 } }
)

> db.students.insert ({
...   _id: 4,
...   grades: [
...      { grade: 80, mean: 75, std: 8 },
...      { grade: 85, mean: 90, std: 5 },
...      { grade: 90, mean: 85, std: 3 }
...   ]
... })
WriteResult({ "nInserted" : 1 })
>
>
> db.students.update(
...    { _id: 4, "grades.grade": 85 },
...    { $set: { "grades.$.std" : 6 } }
... )
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
>
>  db.students.find();
{ "_id" : 4, "grades" : [ { "grade" : 80, "mean" : 75, "std" : 8 }, { "grade" : 85, "mean" : 90, "std" : 6 }, { "grade" : 90, "mean" : 85, "std" : 3 } ] }
>
>
>
> db.students.find().pretty();
{
        "_id" : 4,
        "grades" : [
                {
                        "grade" : 80,
                        "mean" : 75,
                        "std" : 8
                },
                {
                        "grade" : 85,
                        "mean" : 90,
                        "std" : 6
                },
                {
                        "grade" : 90,
                        "mean" : 85,
                        "std" : 3
                }
        ]
}

 若要多个filed 匹配则使用


db.students.update(
   {
     _id: 4,
     grades: { $elemMatch: { grade: { $lte: 90 }, mean: { $gt: 80 } } }
   },
   { $set: { "grades.$.std" : 6 } }
)



我有一个文档对象
{
    "DocEntry": 15,
    "UserSign": 1,
    "Status": 0,
    "Users": [
        {
            "UserSign": 1,
            "Status": 0
        },
        {
            "UserSign": 2,
            "Status": 0
        }
    ]
}
如何用db.collection.Update 来更新Users 嵌入文档array里面的所有Status 为1



db.doc1.remove({});
db.doc1.insert( {
    "DocEntry": 15,
    "UserSign": 1,
    "Status": 0,
    "Users": [
        {
            "UserSign": 1,
            "Status": 0
        },
        {
            "UserSign": 2,
            "Status": 0
        }
    ]
});


db.doc1.insert( {
    "DocEntry": 16,
    "UserSign": 1,
    "Status": 0,
    "Users": [
        {
            "UserSign": 1,
            "Status": 0
        },
        {
            "UserSign": 2,
            "Status": 0
        }
    ]
});


You cannot modify multiple array elements in a single update operation. Thus, you'll have to repeat the update in order to migrate documents which need multiple array elements to be modified. You can do this by iterating through each document in the collection, repeatedly applying an update with $elemMatch until the document has all of its relevant comments replaced, e.g.:

db.collection.find().forEach( function(doc) {
  do {
    db.collection.update({_id: doc._id,
                          comments:{$elemMatch:{user:"test",
                                                avatar:{$ne:"new_avatar.jpg"}}}},
                         {$set:{"comments.$.avatar":"new_avatar.jpg"}});
  } while (db.getPrevError().n != 0);
})


无法通过一个update操作更新多个array元素,所以需要写个循环


db.doc1.find().forEach(function(doc) {
  do {
    db.doc1.update( {"Users.Status":0} ,  {$set:{"Users.$.Status":1 }});
  } while (db.getPrevError().n != 0);
})




此条目发表在 未分类 分类目录。将固定链接加入收藏夹。

评论功能已关闭。