您可以使用以下更新语句更新“act_mgr”数组内嵌入的“sales”文档:
> db.sales.update({"act_mgr.sales.last_interacted":"[email protected] /cdn-cgi/l/email-protection"}, {$push:{"act_mgr.$.sales.agent":"[email protected] /cdn-cgi/l/email-protection"}, $set:{"act_mgr.$.sales.last_interacted":"[email protected] /cdn-cgi/l/email-protection"}})
> db.sales.find().pretty()
{
"_id" : ObjectId("4f855061dd53351011000b42"),
"act_mgr" : [
{
"sales" : {
"agent" : [
"[email protected] /cdn-cgi/l/email-protection",
"[email protected] /cdn-cgi/l/email-protection"
],
"last_interacted" : "[email protected] /cdn-cgi/l/email-protection"
}
}
],
"email" : "[email protected] /cdn-cgi/l/email-protection",
"name" : "Aman",
"sales" : [
{
"sno" : 1,
"message" : "description",
"status" : "open"
},
{
"sno" : 12,
"message" : "assad",
"status" : "open"
}
]
}
>
您可以将包含“开发人员”信息的嵌入文档添加到数组中,如下所示:
> db.sales.update({"_id" : ObjectId("4f855061dd53351011000b42")}, {$push:{"act_mgr":{ "developer" : {"agent" : ["[email protected] /cdn-cgi/l/email-protection" ], "last_interacted" : "[email protected] /cdn-cgi/l/email-protection" } }}})
> db.sales.find().pretty()
{
"_id" : ObjectId("4f855061dd53351011000b42"),
"act_mgr" : [
{
"sales" : {
"agent" : [
"[email protected] /cdn-cgi/l/email-protection",
"[email protected] /cdn-cgi/l/email-protection"
],
"last_interacted" : "[email protected] /cdn-cgi/l/email-protection"
}
},
{
"developer" : {
"agent" : [
"[email protected] /cdn-cgi/l/email-protection"
],
"last_interacted" : "[email protected] /cdn-cgi/l/email-protection"
}
}
],
"email" : "[email protected] /cdn-cgi/l/email-protection",
"name" : "Aman",
"sales" : [
{
"sno" : 1,
"message" : "description",
"status" : "open"
},
{
"sno" : 12,
"message" : "assad",
"status" : "open"
}
]
}
>
有关 $push 和 $set 修饰符的文档可以在“更新”文档中找到:http://www.mongodb.org/display/DOCS/Updating http://www.mongodb.org/display/DOCS/Updating
有关使用 Mongo db 创建和更新嵌入式文档的更多信息,请参阅标题为“点表示法(触及对象)”的文档http://www.mongodb.org/display/DOCS/Dot+Notation+%28Reaching+into+Objects%29 http://www.mongodb.org/display/DOCS/Dot+Notation+%28Reaching+into+Objects%29
有关使用“$”位置运算符更新嵌入文档的信息可以在“更新”文档的“$ 位置运算符”部分中找到。
http://www.mongodb.org/display/DOCS/Updating#Updating-The%24positionaloperator http://www.mongodb.org/display/DOCS/Updating#Updating-The%24positionaloperator
警告:通常更常见的是让嵌入文档全部匹配相同的结构,以便可以更容易地引用各个嵌入文档。您的“sales”数组就是一个很好的例子;每个嵌入文档包含相同的键“sno”、“message”和“status”
但是,“act_mgr”数组中的嵌入文档包含不同的键;第一个包含“销售”,第二个包含“开发人员”。相反,也许可以考虑以下结构:
"act_mgr" : [
{
"title" : "sales",
"agent" : [
"[email protected] /cdn-cgi/l/email-protection",
"[email protected] /cdn-cgi/l/email-protection"
],
"last_interacted" : "[email protected] /cdn-cgi/l/email-protection"
},
{
"title": "developer",
"agent" : [
"[email protected] /cdn-cgi/l/email-protection"
],
"last_interacted" : "[email protected] /cdn-cgi/l/email-protection"
}
]
现在,每个嵌入文档都包含相同的键“title”、“agent”和“last_interacted”。
您可以使用以下命令更新子文档。
> db.sales.update({"act_mgr.title":"sales"}, {$push:{"act_mgr.$.agent":"[email protected] /cdn-cgi/l/email-protection"}, $set:{"act_mgr.$.last_interacted":"[email protected] /cdn-cgi/l/email-protection"}})
希望这将使您能够进行所需的更新,并可能为您提供有关架构设计的一些思考。祝你好运!