Syed Kamran Haider
Syed Kamran Haider

Reputation: 125

MongoDB: Updating multiple using '$in'

Need some expert suggestions on using '$in' while performing 'update' in mongodb collections. Here is an example:

db.users.update({userid: {$in: ['1','2','3']}}, {$inc: {mycounter: 1}}, false, true);

is it better to use '$in' in this scenario? Also can anyone guide me if there is any limit for array count/size for '$in' in my above scenario it might be 10k+ user ids per update..

Thank you.

Upvotes: 1

Views: 2182

Answers (1)

Gates VP
Gates VP

Reputation: 45307

Ran the following simple test from the shell:

for(var i = 0; i < 100000; i++) { db.foo.save({ _id : i, x : 1 }) }
db.foo.count(); // should be 100k

query = []
for(var i = 0; i < 10000; i++) { query.push(i) }
db.foo.update( { _id : {$in:query} }, {$inc : {x:1} }, false, true)

db.foo.find({x:2}).count()  // should be 10k

Performance was reasonable, seems like it can handle 10k simultaneous updates.

I can't find any indication that there's a hard limit on the array size. I think you can apply a "reasonable" limit here though. Even if you can issue commands of with an array of 100k, that's going to be difficult to track.

Some drivers also allow for bulk update commands, which may be more appropriate for what you're doing.

Upvotes: 2

Related Questions