【发布时间】:2017-06-19 01:31:59
【问题描述】:
在MongoDB 的以下文档中需要更新、删除、添加、更新、删除等操作的帮助。
下面是存在于temp 集合中的MongoDB 文档。
{
"local_id" : "1841",
"name_first" : "tiger",
"name_last" : "lion",
"address" : [
{
"id" : 1,
"address_type" : "Home",
"city" : "Delhi",
"country" : "",
"po_box" : ""
},
{
"id" : 2,
"address_type" : "Work",
"city" : "",
"country" : "",
"po_box" : ""
}
],
"email" : [
{
"email_id" : "blah@gmail.com",
"id" : 1,
"type" : "Home"
},
{
"email_id" : "Pearl1@gmail.com",
"id" : 2,
"type" : "Work"
}
],
"phone_number" : [
{
"id" : 1,
"no" : "+911234567890",
"type" : "Mobile"
},
{
"id" : 2,
"no" : "+917894561230",
"type" : "work"
}
]
}`
现在我有一些像下面这样的文档,我想要在上面的文档上比较、添加、更新、删除的查询。
`
{
"local_id" : "1730",
"name_first" : "lion",
"name_last" : "king",
"address" : [
{
"id" : 1,
"address_type" : "Home",
"city" : "Delhi",
"country" : "India",
"po_box" : "110041"
},
{
"id" : 2,
"address_type" : "Work",
"city" : "Delhi-NCR",
"country" : "India",
"po_box" : "110048"
},
{
"id" : 3,
"address_type" : "Work",
"city" : "Delhi-NCR",
"country" : "Indai",
"po_box" : "110048"
}
],
"email" : [
{
"email_id" : "updatethis@gmail.com",
"id" : 1,
"type" : "Home"
},
{
"email_id" : "Pearl1@gmail.com",
"id" : 2,
"type" : "Work"
},
{
"email_id" : "addthisarray@gmail.com",
"id" : 3,
"type" : "personal"
}
],
"phone_number" : [
{
"id" : 1,
"no" : "+911234567890",
"type" : "Mobile"
}
/*second array not here so remove that array from that document*/
]
}`
【问题讨论】:
-
为什么要对它进行比较、添加、更新和删除。如果您想优先考虑第二个文档,只需替换整个文档即可。
-
我还想维护日志详细信息,以便进行比较。
标签: javascript arrays mongodb compare insert-update