$push and $set in same MongoDB update

后端 未结 2 604
粉色の甜心
粉色の甜心 2020-12-29 21:56

I\'m trying to use MongoDB\'s Java driver to make two updates ($set and $push) to a record in the same operation. I\'m using code similar to the following:

         


        
2条回答
  •  别那么骄傲
    2020-12-29 22:17

    My mongodb version is 3.4.20 and while using

    db.collection.update({_id: pageId}, [{$push: {values: dboVital}}, {$set: {endTime: time}}]);
    

    I received error

    [thread1] Error: field names cannot start with $ [$push] :
    

    To solve that error we can use:

    db.collection.update({_id: pageId}, {$push: {values: dboVital}, $set: {endTime: time}});
    

提交回复
热议问题