Py学习  »  MongoDB

Mongodb更新子文档字段的方法比逐个更新更好[重复]

Thomas Kim • 4 年前 • 3737 次点击  

我有一个Mongo文档,其中包含一个元素数组。

我想重置 .handled 数组中所有对象的属性,其中 .profile =XX。

文件格式如下:

{
    "_id": ObjectId("4d2d8deff4e6c1d71fc29a07"),
    "user_id": "714638ba-2e08-2168-2b99-00002f3d43c0",
    "events": [{
            "handled": 1,
            "profile": 10,
            "data": "....."
        } {
            "handled": 1,
            "profile": 10,
            "data": "....."
        } {
            "handled": 1,
            "profile": 20,
            "data": "....."
        }
        ...
    ]
}

所以,我尝试了以下方法:

.update({"events.profile":10},{$set:{"events.$.handled":0}},false,true)

但是它只更新 第一 每个文档中匹配的数组元素。(这就是 $ - the positional operator .)

如何更新 全部的 匹配的数组元素?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/50472
 
3737 次点击  
文章 [ 14 ]  |  最新文章 4 年前