最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - $set and $push for multiple documents in the same mongodb update - Stack Overflow

programmeradmin0浏览0评论

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.

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. I wanted a query that runs on 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

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.

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. I wanted a query that runs on 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

Share Improve this question asked Dec 14, 2017 at 1:48 Hernan EfronHernan Efron 411 silver badge4 bronze badges 4
  • does this answer your question? stackoverflow./questions/38864917/… – Isaac Commented Dec 14, 2017 at 1:51
  • Something like this should work { "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') } } }, { "multi": true } – s7vr Commented Dec 14, 2017 at 2:03
  • Thanks @Veeram! it worked! – Hernan Efron Commented Dec 14, 2017 at 2:57
  • another question I have is how to pass to the "replacedStatus" field not the status "Captured", but dynamically the status that the charge had before the update, do you know what I mean? – Hernan Efron Commented Dec 14, 2017 at 2:59
Add a ment  | 

1 Answer 1

Reset to default 7

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')
                }
            }
        })
发布评论

评论列表(0)

  1. 暂无评论