Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The <strong>query itself is a document</strong> . MongoDB limits document sizes (as of version 2.4.0+) to 16 MB. </p> <p>Really, what you're doing with a find is:</p> <pre><code>db.collectionName.find(queryDoc) </code></pre> <p>where 'queryDoc' is something like:</p> <pre><code>{ 'fieldOne' : { $in : [ 1, 2, 3, 4] } } </code></pre> <p>To find the maximum number of values you can pass to an $in query, use the <strong>bsonsize</strong> command:</p> <pre><code>mongos&gt; Object.bsonsize([1]) 16 mongos&gt; Object.bsonsize({ 'fieldOne' : { $in : [ 1, 2, 3, 4] } }) 74 mongos&gt; Object.bsonsize({ 'fieldOne' : { $in : [ 1, 2, 3, 4, 5] } }) 85 mongos&gt; Object.bsonsize({ 'fieldOne' : { $in : [ 1, 2, 3, 4, 5, 6] } }) 96 </code></pre> <p>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:</p> <pre><code>mongos&gt; Object.bsonsize({ 'fieldOne' : { $in : [ 1, 2, 3, 4, 5, 6, 69000] } }) 107 mongos&gt; Object.bsonsize({ 'fieldOne' : { $in : [ 1, 2, 3, 4, 5, 6, 6900000] } }) 107 mongos&gt; Object.bsonsize({ 'fieldOne' : { $in : [ 1, 2, 3, 4, 5, 6, 69000000000] } }) 107 mongos&gt; Object.bsonsize({ 'fieldOne' : { $in : [ 1, 2, 3, 4, 5, 6, 69000000000000] } }) 107 mongos&gt; Object.bsonsize({ 'fieldOne' : { $in : [ 1, 2, 3, 4, 5, 6, 6900000000000000] } }) 107 mongos&gt; Object.bsonsize({ 'fieldOne' : { $in : [ 1, 2, 3, 4, 5, 6, 690000000000000000] } }) 107 mongos&gt; Object.bsonsize({ 'fieldOne' : { $in : [ 1, 2, 3, 4, 5, 6, 69000000000000000000] } }) 107 mongos&gt; Object.bsonsize({ 'fieldOne' : { $in : [ 1, 2, 3, 4, 5, 6, 6900000000000000000000] } }) 107 mongos&gt; Object.bsonsize({ 'fieldOne' : { $in : [ 1, 2, 3, 4, 5, 6, 69000000000000000000000000] } }) 107 </code></pre> <p>So, no matter what the size of an individual number, it's the same bsonsize.</p> <p><strong>On to the Question Itself: How big is that query document?</strong></p> <p>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:</p> <pre><code>mongos&gt; Object.bsonsize({ 'a' : { '$in' : [1] }}) 34 mongos&gt; Object.bsonsize({ '' : { '$in' : [1] }}) 33 mongos&gt; Object.bsonsize({ '' : { '$in' : [] }}) 22 </code></pre> <ul> <li>The query document itself is 22 bytes;</li> <li>Each byte of the field name adds a single byte;</li> <li>Each number added to the $in clause adds 11 bytes.</li> </ul> <p>So, Presuming you have a one-byte fieldname (the minimum, really), your maximum is:</p> <pre><code>mongos&gt; 16*1024*1024 16777216 mongos&gt; (16*1024*1024) - 22 - 1 16777193 mongos&gt; ((16*1024*1024) - 22 -1) / 11 1525199.3636363635 </code></pre> <p><strong>THE ANSWER: 1,525,198</strong> (That's 1.5 million. That's pretty big.)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload