我正在使用 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"
}
] }
>
...所以基本上点符号没有奏效。