社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  MongoDB

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

Thomas Kim • 5 年前 • 4480 次点击  

我有一个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
 
4480 次点击  
文章 [ 14 ]  |  最新文章 5 年前