3

我正在使用 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"
        }
] }
> 

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

4

1 回答 1

2

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

首先,我使用的是 MongoDB 1.2.2,这个版本不支持上面描述的 update $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 命令一样简单。

于 2012-08-09T08:57:57.840 回答