gpt4 book ai didi

mongodb - 如何按字段值 $pull 数组元素(当元素是对象时)

转载 作者:可可西里 更新时间:2023-11-01 09:46:13 26 4
gpt4 key购买 nike

我正在使用 MongoDB 并尝试从符合条件的数据库中的文档中删除数组元素(它们本身是嵌入的文档)。为此,我尝试在更新命令中使用 $pull 运算符。但在某些情况下我无法完成这项工作(请参阅下面的描述)。我错过了什么?

提前致谢。

-萨钦

> use test
switched to db test

//First, insert a record with an array of addresses, with array elements being embedded objects with exactly 1 element (email)
> db.users.insert({
name: 'smith',
addresses:[{email:'a@b'},{email:'c@d'}]
});... ... ...


//Result of the insertion
> db.users.find()
{ "_id" : ObjectId("50226b46545b516cdbadbcd9"), "name" : "smith", "addresses" : [ { "email" : "a@b" }, { "email" : "c@d" } ] }


//From records with name= Smith, try to $pull any array elements with email a@b

> db.users.update({name:'smith'}, {$pull:{addresses:{email:'a@b'}}});>

//After successful $pull

> db.users.find()
{ "_id" : ObjectId("50226b46545b516cdbadbcd9"), "name" : "smith", "addresses" : [ { "email" : "c@d" } ] }


//Now insert a record with an array of addresses, with array elements being embedded objects with exactly 2 elements (email, phone)

> db.users.insert({
name: 'smith',
addresses:[{email:'a@b', phone: '12345'},{email:'c@d',phone :'54321'}]
});... ... ...


//Result of the insertion

> db.users.find()
{ "_id" : ObjectId("50226b46545b516cdbadbcd9"), "name" : "smith", "addresses" : [ { "email" : "c@d" } ] }
{ "_id" : ObjectId("50226bfc545b516cdbadbcda"), "name" : "smith", "addresses" : [
{
"email" : "a@b",
"phone" : "12345"
},
{
"email" : "c@d",
"phone" : "54321"
}
] }

//From records with name= Smith, again try to $pull any array elements with email a@b

> db.users.update({name:'smith'}, {$pull:{addresses:{email:'a@b'}}})


// - Unsuccessful $pull (Why? How to fix this)
> db.users.find()
{ "_id" : ObjectId("50226b46545b516cdbadbcd9"), "name" : "smith", "addresses" : [ { "email" : "c@d" } ] }
{ "_id" : ObjectId("50226bfc545b516cdbadbcda"), "name" : "smith", "addresses" : [
{
"email" : "a@b",
"phone" : "12345"
},
{
"email" : "c@d",
"phone" : "54321"
}
] }


//Meanwhile, the single element pull still works as before -

> db.users.update({name:'smith'}, {$pull:{addresses:{email:'c@d'}}})


> db.users.find()
{ "_id" : ObjectId("50226b46545b516cdbadbcd9"), "name" : "smith", "addresses" : [ ] }
{ "_id" : ObjectId("50226bfc545b516cdbadbcda"), "name" : "smith", "addresses" : [
{
"email" : "a@b",
"phone" : "12345"
},
{
"email" : "c@d",
"phone" : "54321"
}
] }
>

感谢您的回复,尽管那没有用。这是 Mongo shell 的转录本。

> db.users.find()
{ "_id" : ObjectId("50226b46545b516cdbadbcd9"), "name" : "smith", "addresses" : [ ] }
{ "_id" : ObjectId("50226bfc545b516cdbadbcda"), "name" : "smith", "addresses" : [
{
"email" : "a@b",
"phone" : "12345"
},
{
"email" : "c@d",
"phone" : "54321"
}
] }
> db.users.update({name:'smith'}, {$pull:{"addresses.email": 'a@b'}})
Modifier spec implies existence of an encapsulating object with a name that already represents a non-object, or is referenced in another $set clause
> db.users.find()
{ "_id" : ObjectId("50226b46545b516cdbadbcd9"), "name" : "smith", "addresses" : [ ] }
{ "_id" : ObjectId("50226bfc545b516cdbadbcda"), "name" : "smith", "addresses" : [
{
"email" : "a@b",
"phone" : "12345"
},
{
"email" : "c@d",
"phone" : "54321"
}
] }
>

...所以基本上点符号没有用。

最佳答案

好的,所以我找到了答案。

首先,我使用的是 MongoDB 1.2.2,这个版本不支持如上所述的更新 $pull 操作。

接下来,我升级到 MongoDB 2.06(最新稳定版)。然后当我使用在 1.2.2 中创建的旧数据库时,结果相同。

接下来,我在 2.06 中创建了一个新的数据库,然后尝试了@sergio-tulentsev 的建议,即db.users.update({name:'smith'}, {$pull:{"addresses.email": 'a@b'}})

不幸的是,这也不起作用。

最后,我尝试了我无法执行的初始命令

db.users.update({name:'smith'}, {$pull:{addresses:{email:'a@b'}}})

成功了!!!

所以外卖:1.更新MongoDB服务器2. 旧版本的数据库文件不起作用,只适用于新的数据库文件。现在我需要以某种方式将我的数据迁移到较新的版本。

更新:...这个迁移就像发出 mongod --upgrade 命令一样简单。

关于mongodb - 如何按字段值 $pull 数组元素(当元素是对象时),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11865979/

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