MongoDB 索引和 $or 运算符

标签 mongodb operator-keyword indexing

对不起,如果以前有人问过这个问题。我找不到明确的答案。如果我的查询包含 $or 运算符,我可以查询 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
                (
                )

        )

)

最佳答案

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

> 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
}

关于MongoDB 索引和 $or 运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6872724/

相关文章:

gnuplot - gnuplot 中有 in prod() 运算符吗?

mysql - 在 mysql 的表中索引电子邮件,电话列

python - 如何打印 Pandas 数据框的特定行?

arrays - std.algorithm.remove() 复制数组项?

node.js - 如何将 ObjectId 传递到 POST 请求中?

python - 如何聚合两个集合,其中一个集合的字段大于另一个集合

c++ - 赋值运算符不起作用

c++ - 拆分内联非成员运算符的声明和定义的正确方法(链接器问题)

mongodb - 使用 where 条件聚合查询

java - 查询多种文档类型 Spring Mongo