我在MongoDB :: Collection-> find函数中发现了一个奇怪的行为,想知道这是否是我对驱动程序工作方式误解的结果,这真是一种奇怪的行为。
我想创建一个查询,搜索索引字段,但也搜索其他一些字段。为了确保MongoDB将使用现有索引,我想将搜索项作为array_ref传递,字段按我希望的顺序,但explain()函数显示使用BasicCursor并扫描所有文档。另一方面,如果我将搜索项作为hash_ref(不确保和字段顺序)传递,MongoDB会以正确的顺序获取字段并使用现有索引。
我有一个包含以下文件的集合:
{
"customer" : NumberLong(),
"date" : ISODate(),
field_a : 1,
field_b: 2,
[...]
field_n: 1
}
在字段customer和date上有一个名为customer_1_date_1
的索引。
当我将搜索词作为数组引用时,explain命令指出:
my $SearchTerms = [
'customer', $customer,
'date' , $date ,
'field_a' , $field_a ,
'field_b' , $field_b ,
];
cursor "BasicCursor",
[...]
nscanned 5802,
nscannedAllPlans 5802,
[...]
另一方面,当我将搜索词作为哈希引用时,explain命令指出:
my $SearchTerms = {
customer => $customer,
date => $date,
field_a => $field_a,
field_b => $field_b,
};
allPlans [
[0] {
cursor "BtreeCursor customer_1_date_1",
[...]
n 1,
nscanned 4,
nscannedObjects 4
},
[1] {
cursor "BasicCursor",
[...]
n 0,
nscanned 4,
nscannedObjects 4
}
],
[...]
nscanned 4,
nscannedAllPlans 8,
nscannedObjects 4,
nscannedObjectsAllPlans 8,
所以,我的问题是:我应该相信哈希引用始终获得现有索引吗? 有没有办法,不使用额外的模块,以确保这一点?
谢谢大家,
绢麻