【发布时间】:2012-10-05 21:16:15
【问题描述】:
我的 mongo 文档如下:
{
"_id" : ObjectId("506e9e54a4e8f51423679428"),
"description" : "ffffffffffffffff",
"menus" : [
{
"_id" : ObjectId("506e9e5aa4e8f51423679429"),
"description" : "ffffffffffffffffffff",
"items" : [
{
"name" : "xcvxc",
"description" : "vxvxcvxc",
"text" : "vxcvxcvx",
"menuKey" : "0",
"onSelect" : "1",
"_id" : ObjectId("506e9f07a4e8f5142367942f")
} ,
{
"name" : "abcd",
"description" : "qqq",
"text" : "qqq",
"menuKey" : "0",
"onSelect" : "3",
"_id" : ObjectId("507e9f07a4e8f5142367942f")
}
]
}
]
}
现在我想把它改成:
{
"_id" : ObjectId("506e9e54a4e8f51423679428"),
"description" : "ffffffffffffffff",
"menus" : [
{
"_id" : ObjectId("506e9e5aa4e8f51423679429"),
"description" : "ffffffffffffffffffff",
"items" : {
{
"name" : "xcvxc",
"description" : "vxvxcvxc",
"text" : "vxcvxcvx",
"menuKey" : "0",
"onSelect" : "1",
"_id" : ObjectId("506e9f07a4e8f5142367942f")
} ,
{
"name" : "abcd",
"description" : "qqq",
"text" : "qqq",
"menuKey" : "0",
"onSelect" : "3",
"_id" : ObjectId("507e9f07a4e8f5142367942f")
}
}
}
]
}
这在 mongo 中可能吗?在第一个模式中,不可能自动更新,因为我们不能在更新深层时使用两个“$”。所以我想改变架构和第二个一样,我该如何实现呢?
对于第一个,我使用“$push”在menus中添加items...
任何帮助都会很棒..
【问题讨论】:
-
两者到底有什么区别?在第二个中是否有错字(字段名称必须是字符串)?你说的第一个模式中不可能的更新是什么?
-
在第二种形式中,
items不是有效的 Json 对象。它必须是键值对。 -
@Thilo 实际上我需要进行原子更新,例如:我想将项目中的“name”:“abcd”更新为“name”:“cc”。
-
@GowtGM:那么你可能应该问这个问题。也许这有帮助:stackoverflow.com/questions/7425993/…
-
@Thilo 是的,但不可能这样做,我只能使用索引值进行更新,但我需要使用 id。所以我正在寻找其他模式..