1

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.

1
  • 1
    Well, create a benchmark and try it out. Commented Feb 22, 2011 at 14:06

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I would suggest doing it in batches closer to 1K, but that is just my gut experience. Also, breaking up large updates is never a bad idea.
thanks for the detailed response, you are right, dividing the large set of updates into smaller ones would be a good idea..much appreciated :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.