mongoDB upsert on array

后端 未结 2 1722
温柔的废话
温柔的废话 2020-12-07 03:29

my db model looks like this

\"clientId\":\"123456\"
\"devices\" : 
[{
      \"deviceId\" : \"123\",
      \"deviceType\" : \"ios\",
      \"notificat         


        
2条回答
  •  有刺的猬
    2020-12-07 04:02

    You're looking for a typical positional operator case:

    db.coll.update
    (
        {
            "clientId":"123456","devices.deviceId":"321"
        },
        {
            $set:
            {
                "devices.$.deviceType":"kindle","devices.$.notification":"false"
            }
        }
    )
    

    You need to update an element matched inside an array, $ does this for you, so you can find a document with a specific element inside of an array and "remember" with the positional operator $ which one you matched to update it.

提交回复
热议问题