0

Collection in database:

'_id' : ObjectId("sdhsndi66822dsxsd"),
'values' : {
        "ping" : [ 
            {
                "key" : "5ef9ac6eb1272e16729b17f6",
                "name" : "sdsadsadsa"
            },
           {
                "key" : "5ef9ac6eb1272e16729b17f6",
                "name" : "sdsadsadsa"
            }
              ]
           }

Here I need to add/update a new key value pair. So the expected output should be:

'_id' : ObjectId("sdhsndi66822dsxsd"),
    'values' : {
            "ping" : [ 
                {
                    "key" : "5ef9ac6eb1272e16729b17f6",
                    "name" : "sdsadsadsa",
                    "new":"added_new"
                },
               {
                    "key" : "5ef9ac6eb1272e16729b17f6",
                    "name" : "sdsadsadsa",
                    "new":"added_new"
                }
                  ]
               }

I need to achieve this with pymongo(mongo query also helpful). I have tried using $addtoset but doesnot help.

1
  • If you answer helps you please tick and upvote to help people who seek this kind of question Commented Aug 23, 2020 at 8:45

1 Answer 1

1

You can use $addFields.

[
  {
    $addFields: {
      "values.ping.new": "added_new"
    }
  }
]

Working Mongo playground

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.