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

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

+46
mongodb
Mar 16 2018-11-11T00:
source share
3 answers

Itself is a document . MongoDB limits document sizes (starting with version 2.4.0+) to 16 MB.

Indeed, what do you do with the search:

db.collectionName.find(queryDoc) 

where the 'queryDoc' looks something like this:

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

To find the maximum number of values ​​that you can pass in a $ request, 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 each additional integer is 11 bytes. Not 11 bits, 11 bytes. This is because BSON internally stores numbers of at least 64 bits, plus a shell. 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 

Thus, regardless of the size of the individual number, it is the same bsonsize.

To the question: how large is this query document?

Add them for a single request field with the $ in clause in pymongo, via the javascript mongos hint, regardless of whether you all do the same extra facts for the maximum size of the $ request:

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

So, suppose you have a single-byte field name (minimum, really), your maximum:

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

ANSWER: 1,525,198 (That's 1,5 million. It's pretty big.)

+50
Aug 28 '13 at 18:23
source share

There seems to be no limit.

I did a little test.

1) In collection A there was - 1 million simple JSON object {id :, name:}

2) In Collection B, I loaded the reference identifiers of Collection A until I received the following exception. I could insert a reference counter 450 thousand. 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 identifiers in $ to [id1 ... id450000] and pull out the entire list of 450 thousand identifiers from 1 million objects in collection A.

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

+31
Mar 17 2018-11-11T00:
source share

I think the restriction is determined only by the size of the BSONDocument. When you define a query, you can continue to add values ​​to the $ in clause until you exceed the maximum document size. So, how many values ​​you can have in a sentence depends on how large each value is (the smaller the size of each value, the more you can include $ in in a sentence).

In terms of performance, from what I found there is a “sweet spot” for the number of values ​​in the $ in article. See My answer in this related question: Is it possible to query MongoDB several times per request?

i.e. the balancing number of values ​​in the $ in position and the number of requests sent. I am in the middle of a blog post to try to dive into more details.

+6
Mar 17 2018-11-11T00:
source share



All Articles