我如何$按字段值拉取数组元素(当元素是对象时)

时间:2012-08-08 13:49:45

标签: mongodb

我正在使用MongoDB并尝试从符合条件的数据库中的文档中删除数组元素(自身嵌入的文档)。对此我尝试在update命令中使用$ pull运算符。但在某些情况下我无法完成这项工作(见下面的说明)。我错过了什么?

提前致谢。

-Sachin

> 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"
        }
] }
> 

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

1 个答案:

答案 0 :(得分:1)

好的,我找到了答案。

首先,我使用的是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:{address:{email:'a @ b'}}})

它有效!!!

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

更新:...此迁移就像发布mongod --upgrade命令一样简单。