1

我在 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_1_date_1客户和日期字段的索引。

当我将搜索词作为数组 ref 给出时,解释命令指出:

my $SearchTerms = [
    'customer', $customer, 
    'date'    , $date    , 
    'field_a' , $field_a , 
    'field_b' , $field_b , 
];

cursor                    "BasicCursor",
[...]
nscanned                  5802,
nscannedAllPlans          5802,
[...]

另一方面,当我将搜索词作为哈希引用给出时,解释命令指出:

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,

所以,我的问题是:我应该相信哈希 ref 总是得到现有的索引吗?有什么方法可以在不使用其他模块的情况下确保这一点?

谢谢你们,

娟妈

4

0 回答 0