$ set和$ push在同一个mongodb更新中的多个文档

问题描述 投票:0回答:1

我努力获得更新的同一集合的多个文档,需要将字段添加到JSON“root”,以及一个名为logs的现有数组的新对象。

我正在尝试这个

db.getCollection('charges').
update({'supports.dest':ObjectId("xyz"), 
      date:{
                          $gte: new Date(2017, 11),
                          $lt: new Date(2017, 12) 
           },
      "status": {$nin : ["Captured"]  
                }
        },
      {$set: {"status": "BillingSuspended",  
           "replacedStatus":"Captured"} 
      },
      {multi:true},
      {$push : 
        {logs : {"replacedStatus" : "Captured" ,
                 date: new Date ('2017-12-13T22:00:00.000Z') 
                }
        }
      }
)

我正在收到这个错误,我尝试取出多个:true然后我松开了允许我同时更新许多文档的多属性。我想要一个在Robomongo上运行的查询。如果你能帮助我,我会感激你们。

错误:

[Failed to execute script.

Error: Fourth argument must be empty when specifying upsert and multi with an object. :
DBCollection.prototype._parseUpdate@src/mongo/shell/collection.js:522:1
DBCollection.prototype.update@src/mongo/shell/collection.js:552:18
@(shell):1:1]

1

javascript mongodb robo3t
1个回答
1
投票

你必须这样试试

db.getCollection('charges').updateMany(
        {
            'supports.dest': ObjectId("xyz"),
            'date': {
                '$gte': new Date(2017, 11),
                '$lt': new Date(2017, 12)
            },
            "status": {
                $nin: ["Captured"]
            }
        },
        {
            $set: {
                "status": "BillingSuspended",
                "replacedStatus": "Captured"
            },
            $push: {
                logs: {
                    "replacedStatus": "Captured",
                    date: new Date('2017-12-13T22:00:00.000Z')
                }
            }
        })
© www.soinside.com 2019 - 2024. All rights reserved.