如何在mongodb中删除深层嵌套对象 [英] How to delete a deep nested object in mongodb
本文介绍了如何在mongodb中删除深层嵌套对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
说我有一个代表这样的书的文档:
Say I have a document that represents books like this:
{
"_id":"1234567890",
"title":"Lord Of The Rings",
"books": {
"1234567890":{
"_id":"123456789890",
"title":"The Two Towers",
"page_count":{
"en":6000,
"de":7000
}
},
"2234567890":{
"_id":"223456789890",
"title":"The Return Of The King",
"page_count":{
"en":6000,
"de":7000
}
},
}
}
我该如何删除第二本书的页数?我尝试过
How would I go about removing the page count for the 2nd book? I tried
{$unset : {"books.2234567890.page_count":""}}
没用.有什么想法吗?
非常感谢
推荐答案
我刺了一下它,看来您要尝试执行的操作应该可以正常工作.我会检查您的查询以找到要更新的正确文档,并确保它找到了您想要的.
I took a stab at it and it looks like what you are trying to do should work correctly. I would check your query to locate the proper document to update and make sure it's finding what you want.
> db.books.findOne()
{
"_id" : "1234567890",
"title" : "Lord Of The Rings",
"books" : {
"1234567890" : {
"_id" : "123456789890",
"title" : "The Two Towers",
"page_count" : {
"en" : 6000,
"de" : 7000
}
},
"2234567890" : {
"_id" : "223456789890",
"title" : "The Return Of The King",
"page_count" : {
"en" : 6000,
"de" : 7000
}
}
}
}
> db.books.update({'_id': "1234567890"}, {$unset: {'books.2234567890.page_count': ""}})
> db.books.findOne()
{
"_id" : "1234567890",
"books" : {
"1234567890" : {
"_id" : "123456789890",
"title" : "The Two Towers",
"page_count" : {
"en" : 6000,
"de" : 7000
}
},
"2234567890" : {
"_id" : "223456789890",
"title" : "The Return Of The King"
}
},
"title" : "Lord Of The Rings"
}
>
这篇关于如何在mongodb中删除深层嵌套对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文