MongoDB索引和$或运算符

时间:2011-07-29 11:55:38

标签: mongodb operator-keyword indexing

很抱歉,如果之前有人询问过。我找不到明确的答案。如果我的查询包含$或运算符,我可以在mongodb索引上查询吗?我的查询看起来像这样:

    // find everything in the collection
    $cursor = $collection->find(array(
   '$or' => array( 
        array('album_id' => array(
            '$in' => $this->my_album_ids
            ),
        'type' => array(
            '$in' => array('like','comment')
            )
         ),
       array(
    'user_id' => (int)session_item('user_id'),
        'type' => 'message',
        'reply' => 'no'
         )
       ),
        'timestamp' => array('$gt' => (int)$since)))->sort(array('timestamp'=>-1))->skip($start)->limit($amount);  

示例在PHP中,但我想这适用于任何语言。

更新

以下是我的索引,但上面的查询不使用它们。虽然看起来对我不错。

    $collection->ensureIndex(array(
        'album_id' => 1,
        'type' => 1,
        'timestamp' => -1,
    ));

    $collection->ensureIndex(array(
        'user_id' => 1,
        'type' => 1,
        'reply' => 1,
        'timestamp' => -1,
    ));

这是我的解释()

Array
(
    [cursor] => BasicCursor
    [nscanned] => 12
    [nscannedObjects] => 12
    [n] => 6
    [scanAndOrder] => 1
    [millis] => 0
    [nYields] => 0
    [nChunkSkips] => 0
    [isMultiKey] => 
    [indexOnly] => 
    [indexBounds] => Array
        (
        )

    [allPlans] => Array
        (
            [0] => Array
                (
                    [cursor] => BasicCursor
                    [indexBounds] => Array
                        (
                        )

                )

        )

    [oldPlan] => Array
        (
            [cursor] => BasicCursor
            [indexBounds] => Array
                (
                )

        )

)

2 个答案:

答案 0 :(得分:8)

是的,$或查询将根据需要使用索引。例如:

> db.test.ensureIndex({a:1})
> db.test.ensureIndex({b:1})
> db.test.find({$or:[{a:1}, {b:2}]}).explain()
{
        "clauses" : [
                {
                        "cursor" : "BtreeCursor a_1",
                        "nscanned" : 0,
                        "nscannedObjects" : 0,
                        "n" : 0,
                        "millis" : 0,
                        "nYields" : 0,
                        "nChunkSkips" : 0,
                        "isMultiKey" : false,
                        "indexOnly" : false,
                        "indexBounds" : {
                                "a" : [
                                        [
                                                1,
                                                1
                                        ]
                                ]
                        }
                },
                {
                        "cursor" : "BtreeCursor b_1",
                        "nscanned" : 0,
                        "nscannedObjects" : 0,
                        "n" : 0,
                        "millis" : 1,
                        "nYields" : 0,
                        "nChunkSkips" : 0,
                        "isMultiKey" : false,
                        "indexOnly" : false,
                        "indexBounds" : {
                                "b" : [
                                        [
                                                2,
                                                2
                                        ]
                                ]
                        }
                }
        ],
        "nscanned" : 0,
        "nscannedObjects" : 0,
        "n" : 0,
        "millis" : 1
}

答案 1 :(得分:4)

Mongo在排序的OR查询中不使用IndexBounds。 试试

db.test.find({$or:[{a:1}, {b:2}]}).sort(somesort).explain();

和IndexBounds与$minElement$maxElement

一起使用