Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the maximum number of parameters passed to $in query in MongoDB?

Tags:

mongodb

What is the maximum number of parameters passed to $in query in MongoDB?

like image 358
Deepan Avatar asked Mar 16 '11 20:03

Deepan


People also ask

What does $IN do in MongoDB?

The $in operator selects the documents where the value of a field equals any value in the specified array. To specify an $in expression, use the following prototype: { field: { $in: [<value1>, <value2>, ... < valueN> ] } } For comparison of different BSON type values, see the specified BSON comparison order.

How does MongoDB define maximum number of documents in collection?

Unless you create a capped collection, there is no limit to the number of documents in a collection. There is a 16MB limit to the size of a document (use gridfs in this situation) and limits in the storage engine for the size of the database and data.

How many records can MongoDB handle?

Mongo can easily handle billions of documents and can have billions of documents in the one collection but remember that the maximum document size is 16mb. There are many folk with billions of documents in MongoDB and there's lots of discussions about it on the MongoDB Google User Group.


2 Answers

The query itself is a document . MongoDB limits document sizes (as of version 2.4.0+) to 16 MB.

Really, what you're doing with a find is:

db.collectionName.find(queryDoc) 

where 'queryDoc' is something like:

{ 'fieldOne' : { $in : [ 1, 2, 3, 4] } } 

To find the maximum number of values you can pass to an $in query, use the bsonsize command:

mongos> Object.bsonsize([1]) 16 mongos> Object.bsonsize({ 'fieldOne' : { $in : [ 1, 2, 3, 4] } }) 74 mongos> Object.bsonsize({ 'fieldOne' : { $in : [ 1, 2, 3, 4, 5] } }) 85 mongos> Object.bsonsize({ 'fieldOne' : { $in : [ 1, 2, 3, 4, 5, 6] } }) 96 

So, you can see that every additional integer is size 11 bytes. Not 11 bits, 11 BYTES. This is due to the way that BSON internally stores numbers as at least 64 bits each, plus the wrapper. This can be easily seen with:

mongos> Object.bsonsize({ 'fieldOne' : { $in : [ 1, 2, 3, 4, 5, 6, 69000] } }) 107 mongos> Object.bsonsize({ 'fieldOne' : { $in : [ 1, 2, 3, 4, 5, 6, 6900000] } }) 107 mongos> Object.bsonsize({ 'fieldOne' : { $in : [ 1, 2, 3, 4, 5, 6, 69000000000] } }) 107 mongos> Object.bsonsize({ 'fieldOne' : { $in : [ 1, 2, 3, 4, 5, 6, 69000000000000] } }) 107 mongos> Object.bsonsize({ 'fieldOne' : { $in : [ 1, 2, 3, 4, 5, 6, 6900000000000000] } }) 107 mongos> Object.bsonsize({ 'fieldOne' : { $in : [ 1, 2, 3, 4, 5, 6, 690000000000000000] } }) 107 mongos> Object.bsonsize({ 'fieldOne' : { $in : [ 1, 2, 3, 4, 5, 6, 69000000000000000000] } }) 107 mongos> Object.bsonsize({ 'fieldOne' : { $in : [ 1, 2, 3, 4, 5, 6, 6900000000000000000000] } }) 107 mongos> Object.bsonsize({ 'fieldOne' : { $in : [ 1, 2, 3, 4, 5, 6, 69000000000000000000000000] } }) 107 

So, no matter what the size of an individual number, it's the same bsonsize.

On to the Question Itself: How big is that query document?

Adding these up for a one field query with an $in clause, in pymongo, through the mongos javascript prompt, whatever, all yeild the same addition facts for the maximum size of an $in query:

mongos> Object.bsonsize({ 'a' : { '$in' : [1] }}) 34 mongos> Object.bsonsize({ '' : { '$in' : [1] }}) 33 mongos> Object.bsonsize({ '' : { '$in' : [] }}) 22 
  • The query document itself is 22 bytes;
  • Each byte of the field name adds a single byte;
  • Each number added to the $in clause adds 11 bytes.

So, Presuming you have a one-byte fieldname (the minimum, really), your maximum is:

mongos> 16*1024*1024 16777216 mongos> (16*1024*1024) - 22 - 1  16777193 mongos> ((16*1024*1024) - 22 -1) / 11 1525199.3636363635 

THE ANSWER: 1,525,198 (That's 1.5 million. That's pretty big.)

like image 141
Kevin J. Rice Avatar answered Sep 30 '22 18:09

Kevin J. Rice


Looks like there is no limitation.

I did a small test.

1) Collection A had - 1 Million simple JSON object {id:,name:}

2) In Collection B, I loaded reference ids of collection A till I got the following exception. I could insert a reference count of 450k of max.

Exception in thread "main" com.mongodb.MongoInternalException: DBObject of size 18388885 is over Max BSON size 16777216 

3) I could send 450k of these ids as $in[id1...id450000] and pull the whole list of 450k ids from 1 Million objects in collection A.

Wow! this is more more more than enough for my application :D. MongoDB is really cool.

like image 20
Deepan Avatar answered Sep 30 '22 18:09

Deepan