简体   繁体   English

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

[英]$set and $push for multiple documents in the same mongodb update

I struggling to get multiple documents of the same collection updated that need to have fields added to the JSON "root", as well as a new object to an existent array called logs . 我在努力更新同一集合的多个文档,这些文档需要将字段添加到JSON“ root”,以及一个存在的名为logs的数组的新对象。

I'm trying this 我正在尝试

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') 
                }
        }
      }
)

I'm getting this error bellow and I tried taking out the multi:true but then I loose the multi property that allows me to update many documents at the same time. 我遇到此错误,然后尝试取出multi:true,但随后放宽了multi属性,该属性使我可以同时更新许多文档。 I wanted a query that runs on Robomongo. 我想要在Robomongo上运行的查询。 I'd appreciate guys if you can help me. 如果您能帮助我,我将不胜感激。

Error: 错误:

[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 1

You have to try like this 你必须这样尝试

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')
                }
            }
        })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM