我有一个 MongoDB 文档,用于保存字典中出现的某些内容:
{
"id" : 1,
"occurrences" : {
"1" : 1,
"2" : 5,
"17" : 1,
"35" : 4
}
}
我现在想要添加或更新一些条目,例如向出现的次数添加“12:3”,或者将“17”的出现次数更新为 2,所以假设我想添加 {"12" :3} 和 {"17":2} 到这本字典。
这让我发疯,因为我根本无法让它工作。我可以使用数组等,但我真的很想在这个特定的设计中使用它。关于如何解决这个问题有什么想法吗?我用 $set 和 $each 等尝试了很多不同的东西。
from mongoDb网站 http://docs.mongodb.org/manual/reference/operator/update/set/查看“在嵌入文档中设置字段”
To specify a <field> in an embedded document or in an array, use dot notation.
For the document matching the criteria _id equal to 100, the following
operation updates the make field in the details document:
db.products.update(
{ _id: 100 },
{ $set: { "details.make": "zzz" } })
在你的情况下
db.collection.update(
{_id:ObjectId("1")},
{ $set: { "occurrences.12": "3", "occurrences.17": "2" }})
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)