gpt4 book ai didi

mongodb - 如何删除mongodb中的深层嵌套对象

转载 作者:行者123 更新时间:2023-12-02 00:32:27 25 4
gpt4 key购买 nike

假设我有一个代表这样的书籍的文档:

{
"_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
}
},
}
}

我该如何删除第二本书的页数?我试过了

{$unset : {"books.2234567890.page_count":""}}

没用。有什么想法吗?

非常感谢

最佳答案

我尝试了一下,看起来您正在尝试做的事情应该可以正常工作。我会检查您的查询以找到要更新的正确文档,并确保它找到您想要的内容。

> 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 - 如何删除mongodb中的深层嵌套对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14421017/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com