Update array with multiple conditions in mongodb

后端 未结 1 1494
深忆病人
深忆病人 2020-12-21 11:54

I have this document:

{
    \"_id\" : ObjectId(\"5b673f525ef92ec6ef16504e\"),
    \"events\" : [ 
        {
            \"name\" : \"Winner\",
            \"         


        
相关标签:
1条回答
  • 2020-12-21 12:32

    When there are multiple conditions to match inside an array then the .Dot notation doesn't work with update query.

    You need to use $elemMatch to match exact two fields inside an array

    db.getCollection('test').updateOne(
      {
        "_id": ObjectId("5b673f525ef92ec6ef16504e"),
        "events": { "$elemMatch": { "name": "Winner", "map": 2 }}
      },
      {
        "$push": { "events.$.something": { "something": "test" }}
      }
    )
    
    0 讨论(0)
提交回复
热议问题