GJa*_*ain 3 mongodb mongodb-query
我们可以使用$push(向数组中添加元素)更新atomically update包含该数组的单个文档
但是,我找不到原子add a new key地映射到文档中的地图的方法。
我可以
* read the document,
* read the map in it,
* update the map in my code and
* update the document in my code.
Run Code Online (Sandbox Code Playgroud)
但这不是原子的。
我只处理一个,single document但是该文档有一张地图。
有没有办法我可以(add a new key)自动更新地图?
取得以下文件:
{
"_id": 1,
"map": {
"field2": 1
}
}
Run Code Online (Sandbox Code Playgroud)
为了将“ field3”添加到地图,您需要像这样进行更新:
db.collection.update({ "_id": 1 }, { "$set": { "map.field3": 2 } })
Run Code Online (Sandbox Code Playgroud)
因此,现在您的文档如下所示:
{
"_id": 1,
"map": {
"field2": 1,
"field3": 2
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1389 次 |
| 最近记录: |