I am using MongoDB and trying to remove array elements (themselves embedded documents) from documents in a DB matching a criteria. FOr this I am trying to use the $pull operator in the update command. But I am unable to make this work in some cases (See description below). What am I missing?
Thanks in advance.
-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"
}
] }
>
Thanks for the resposen, although that didn’t work. Here is the transcript of the 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"
}
] }
>
…so basically the dot notation didnt work out.
Ok, so I found the answer.
First thing, I was using MongoDB 1.2.2, and this version didnt support the update $pull operation as described above.
Next, I upgraded to MongoDB 2.06 (latest stable). Then when I use the old database created in 1.2.2, the same result.
Next, I created a new DB in 2.06 and then tried the suggestion by @sergio-tulentsev, i.e.
db.users.update({name:’smith’}, {$pull:{“addresses.email”: ‘a@b’}})
Unfortunately, this didnt work either.
Last, I tried the initial command I had not been able to execute
db.users.update({name:’smith’}, {$pull:{addresses:{email:’a@b’}}})
And it worked!!!
So takeaway:
1. Update MongoDB server
2. Old version of Database file wont work, only works with new database file. Now I need to somehow migrate my data to the newer version.
UPDATE:…this migration was as simple as issuing the mongod –upgrade command.