Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django Mongodb什么更快$in或$or?_Django_Mongodb_Mongoengine - Fatal编程技术网

Django Mongodb什么更快$in或$or?

Django Mongodb什么更快$in或$or?,django,mongodb,mongoengine,Django,Mongodb,Mongoengine,哪个查询会更快? ObjectId列表的长度可以大于2 db.house.find( { persons: { $elemMatch: { person_id: { $in: [ ObjectId("570028671d41c8eaeb6c9ce8"), ObjectId("570028681d41c8eaeb6c9e38")

哪个查询会更快? ObjectId列表的长度可以大于2

db.house.find(
    { persons:
        { $elemMatch:
            { person_id:
                { $in:
                    [ ObjectId("570028671d41c8eaeb6c9ce8"), 
                      ObjectId("570028681d41c8eaeb6c9e38")
                    ]
                 }
             }
         }
     })

db.house.find(
    { $or:
        [{ persons:
            {$elemMatch:  
                {person_id:ObjectId("570028671d41c8eaeb6c9ce8")}}
         }, 
         {persons:{$elemMatch:              
                {person_id:ObjectId("570028681d41c8eaeb6c9e38")}}
         }]
    })
我有:persons,persons.person_id的索引。如果有问题,我会从Django(mongoengine)进行查询。目前,我的db中大约有10万栋房子和2万人。 众议院文件如下:

{
    "_id" : ObjectId("570031aa1d41c8ed54393b19"),
    "persons" : [
        {
            "person_id" : ObjectId("570028671d41c8eaeb6c9dff"),
            "t" : "150 t"
        },
        {
            "person_id" : ObjectId("5700312d1d41c8ed54393b05"),
            "t" : "1 g"
        },
        {
            "person_id" : ObjectId("5700312d1d41c8ed54393b06"),
            "t" : "70 y"
        }
    ]
}
如中所述,应尽可能使用中的$而不是
$或

但是,在直接与单个字段进行匹配时,也不需要使用
$elemMatch
,因此可以将查询简化为:

db.house.find(
    { 'persons.person_id':
        { $in:
            [ ObjectId("570028671d41c8eaeb6c9ce8"), 
              ObjectId("570028681d41c8eaeb6c9e38")
            ]
        }
    })
如中所述,应尽可能使用中的$而不是
$或

但是,在直接与单个字段进行匹配时,也不需要使用
$elemMatch
,因此可以将查询简化为:

db.house.find(
    { 'persons.person_id':
        { $in:
            [ ObjectId("570028671d41c8eaeb6c9ce8"), 
              ObjectId("570028681d41c8eaeb6c9e38")
            ]
        }
    })

你能出示一份样本文件吗?此外,您可以始终使用
var start=new Date().getTime();doYouStuff;打印(new Date().getTime()-start)
测量差异,或使用@MarkusWMahlberg将示例添加到postCan显示示例文档?此外,您可以始终使用
var start=new Date().getTime();doYouStuff;打印(new Date().getTime()-start)
以测量差异,或使用@MarkusWMahlberg将示例添加到post