I have some code that deletes multiple objects from Mongo using an $or opperator, or at least it should. This code illustrates basically what my app is doing
lookup = []
listofids = ['4e86fee56607f2a8a2000002','4e86fee56607f2a8a2000003','4e86feeb6607f2a8a2000006']
for id in listofids:
lookup.append({'_id':core.ObjectId(id)})
r = self.db().photos.update({
"$or":lookup
},{
'$set':{'var':'value'}
})
That doesn’t work, it doesn’t update any documents. However if I change the code to do a basic find
lookup = []
listofids = ['4e86fee56607f2a8a2000002','4e86fee56607f2a8a2000003','4e86feeb6607f2a8a2000006']
for id in listofids:
lookup.append({'_id':core.ObjectId(id)})
r = self.db().photos.find({
"$or":lookup
})
for rs in r:
self.write(str(rs)+"\n\n")
it outputs all the records. How do I get Mongo to update the records?
self.db() maps to a MongoDB database, photos is the collection.
This code is in Python, but I would imagine the syntax is easy enough to follow
You need to set the
multiparameter toTruein order to update all documents that match your query. Otherwise, it only updates the first one (which you probably just don’t notice). See Collection.update.(Also note: if you’re querying on multiple values of a single field, you can use the $in operator.)