MongoDB中传递给$ in查询的$参数的最大数量是多少? [英] What is the maximum number of parameters passed to $in query in MongoDB?

查看:923
本文介绍了MongoDB中传递给$ in查询的$参数的最大数量是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MongoDB中传递给$ in查询的参数的最大数量是多少?

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

推荐答案

查询本身是一个文档. MongoDB将文档大小(从2.4.0+版本开始)限制为16 MB.

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)

其中"queryDoc"类似于:

where 'queryDoc' is something like:

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

要查找可传递给$ in查询的最大值数量,请使用 bsonsize 命令:

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

因此,您可以看到每个其他整数均为11个字节.不是11位,而是11个字节.这是由于BSON在内部将数字和包装器分别存储为至少64位的方式.可以很容易地看到这一点:

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

因此,无论单个数字的大小如何,其bsonsize都是相同的.

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

问题本身:该查询文档有多大?

通过mongos javascript提示符在pymongo中使用$ in子句将这些字段加到一个字段查询中,无论如何,对于$ in查询的最大大小,它们都具有相同的加法事实:

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

  • 查询文档本身为22个字节;
  • 字段名称的每个字节都添加一个字节;
  • 添加到$ in子句中的每个数字都会增加11个字节.
  • 因此,假设您有一个一字节的字段名(实际上是最小值),则最大值为:

    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
    

    答案:1,525,198 (这是150万.这相当大.)

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

    这篇关于MongoDB中传递给$ in查询的$参数的最大数量是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆