mongodb - Upsert with $in -
i want following functionality in mongodb. have collection named 'entities', stores entity text , it's count. collection looks this:
[{u'_id': u'facebook', u'n': 120}, {u'_id': u'florida', u'n': 98}, {u'_id': u'vegas', u'n': 94}, {u'_id': u'andrew_mason', u'n': 93}, {u'_id': u'obama', u'n': 85}, {u'_id': u'twitter', u'n': 81}, {u'_id': u'outlook', u'n': 81}, {u'_id': u'delhi', u'n': 75}, {u'_id': u'google', u'n': 74}, {u'_id': u'virginia', u'n': 71}]
now, want update collection new entities. basically, i'll have new entities ready in array this:
entityset = ['google', 'abhishek_vaid', 'andrew_mason']
my intentions that, entities in collection count should updated. entities not in collection, count should initialized 1. want use single mongodb query achieve both these effects. till i've been able find following query : (it's in pymongo flavor, it's nonetheless mongodb query)
entity_db_handle.entities.update (
{'_id' : {'$in' : entityset} }, {'$inc' : {'n' : 1} }, upsert=true, multi=true )
however, query updates existing entity counts , not push new entities.
any ideas on this?
certainly work :
> db.test.save({_id:"a", n:3}) > db.test.update({_id:"b"}, {$inc:{n:1}}, true, false) > db.test.find() { "_id" : "a", "n" : 3 } { "_id" : "b", "n" : 1 }
but example query doesn't use $in i'm assuming you're trying :
{'_id' : {$in:['google', 'abhishek_vaid', 'andrew_mason']}, {'$inc' : {'n' : 1} }, upsert=true, multi=true )
and expect update or create entry each _id value pass. not work if of _id values exists since upsert create new document of no document matches search criteria.
Comments
Post a Comment