Removing multiple items from an embedded array, by id, in one operation

3,089 views
Skip to first unread message

Nathan

unread,
Oct 27, 2010, 3:34:19 AM10/27/10
to mongodb-user
Hello, I am wondering if this is possible. If I have an object like
this:

{ _id : ObjectId('0'), pets : [
{ _id : ObjectId(1), name : "something"},
{_id : ObjectId(2), name : "another thing"},
... more things
] }

And I want to remove items with _id ObjectId(1) and ObjectId(2)
(obviously those aren't valid object ids)

I was hoping I could do something like this but I can't get it to
work:

db.my_thing.update( { _id : ObjectId(0) }, {"$pullAll" : {"pets" :
[ { "$elemMatch" : ObjectId(1) }, {"$elemMatch" : ObjectId(2) }]}})

Also I tried this

db.my_thing.update( { _id : ObjectId(0) }, {"$pullAll" : {"pets" :
[ { _id : ObjectId(1)}, { _id : ObjectId(2) } ] } } )

And this

db.my_thing.update( { _id : ObjectId(0) }, {"$pull" : {"pets" :
{"$elemMatch" : { _id : [ObjectId(1), ObjectId(2) ] } } } } )

Is what I'm trying to do possible? I can't use positional ($) operator
or anything like that because in the same command I may be performing
a variety of other set, push, etc. operations on the parent document
that don't involve the "pets" key.

I will have the array index of each embedded document I want to remove
if that would help, but I couldn't figure a way to use that piece of
information.

If this is not possible, I presume the only alternative would be to
batch up "pulls" and issue them as separate commands at the end of my
update? I'm trying to update a document based on changes to an entity
in an ODM, so I'm trying to construct a single command that can effect
all possible changes (sets, unsets, pushes and pulls) all at once

Thanks for your time,

Nathan

Tony Hannan

unread,
Oct 27, 2010, 9:29:07 AM10/27/10
to mongod...@googlegroups.com
$pull with $elemMatch and $in does not seem to work. I reported the bug here:

--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.


Kristina Chodorow

unread,
Oct 27, 2010, 9:52:21 AM10/27/10
to mongod...@googlegroups.com
Assuming your nested fields have _id values id1 and id2, you can do:

db.my_thing.update({},{$pull:{'pets':{_id:{$in:[id1,id2]}}}})

Also, btw, ObjectId('2') isn't a valid ObjectId.  ObjectIds are a special type, they aren't just unique values.  You can just say _id:2 if you want to use ints.

Nathan

unread,
Oct 27, 2010, 12:33:53 PM10/27/10
to mongodb-user
Kristina, that did the trick! I'm actually beaming. Thank you.

Kristina Chodorow

unread,
Oct 27, 2010, 1:35:35 PM10/27/10
to mongod...@googlegroups.com
No problem :)


On Wed, Oct 27, 2010 at 12:33 PM, Nathan <nathan...@hsihealth.com> wrote:
Kristina, that did the trick! I'm actually beaming. Thank you.

--

ta...@codecraft.co.in

unread,
Jun 24, 2018, 3:40:06 AM6/24/18
to mongodb-user
Thanks!
Reply all
Reply to author
Forward
0 new messages