簡體   English   中英

$ set和$ push用於同一mongodb更新中的多個文檔

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

我在努力更新同一集合的多個文檔,這些文檔需要將字段添加到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') 
                }
        }
      }
)

我遇到此錯誤,然后嘗試取出multi:true,但隨后放寬了multi屬性,該屬性使我可以同時更新許多文檔。 我想要在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

你必須這樣嘗試

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