kon*_*nse 7 dictionary upsert mongodb
我有一个MongoDB文档,可以在字典中保存一些内容:
{
  "id" : 1,
  "occurrences" : {
    "1" : 1,
    "2" : 5,
    "17" : 1,
    "35" : 4
  }
}
我现在想要添加或更新一些条目,例如在事件中添加"12:3",或者将"17"的出现次数更新为2,所以让我们假设我要添加{"12" :3}和{"17":2}到这本词典.它让我发疯,因为我根本无法让它发挥作用.我可以使用数组和所有这些,但我真的想在这个特定的设计中使用它.关于如何解决这个问题的任何想法?我用$ set和$ each等尝试了很多不同的东西.
Dan*_*anM 10
从mongoDb网站查看"在嵌入式文档中设置字段"
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" }})