如何更新 mongo db 中具有给定值的指定字段

How to update a specified field having a given value in mongo db

这里我想更新给定的文档如下

{
    
    "emailid": "xxx.gmail",
    
    "user_devices": [],
    "devices": [{
        "created_time": 1607153351,
        "token": 123
    },
    {
        "created_time": 1807153371,
        "token": 1345
    }]
}

这里我需要用一个像“Newfield”:“newfield”这样的新字段更新带有 1345 令牌的现场设备,最终输出如下所示

{
    
    "emailid": "xxx.gmail",
    
    "user_devices": [],
    "devices": [{
        "created_time": 1607153351,
        "token": 123
    },
    {
        "created_time": 1807153371,
        "token": 1345,
        "Newfield":"newfield"

    }]
}

如何像这样更新 mongo 数据库。预先感谢您的回答。

db.products.update({
  "devices.token": 1345 //Matching condition
},
{
  $set: {
    "devices.$.t": 2 //Updates the matched element in the array
  }
})

您可以使用 positional operator - Reference