Nathan
unread,Oct 27, 2010, 3:34:19 AM10/27/10Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
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