Mongodb 条件与 $and 运算符和没有运算符的区别 [英] Mongodb difference between condition with $and operator and without the operator

查看:76
本文介绍了Mongodb 条件与 $and 运算符和没有运算符的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

形式上Mongodb的条件有区别吗

Is there a difference between condition for Mongodb in form

{$and: [{a: 'aaa'}, {b: 'bbb'}]} 

和没有 $and 运算符的 sam 条件

and the sam condition without $and operator

{a: 'aaa', b: 'bbb'}

有人告诉我 $and 运算符会减慢查询速度.这是真的吗?谢谢.

Somebody told me the $and operator slows the query. Is it true? Thanks.

推荐答案

这些条件之间没有区别.回答此类问题的最佳方法是查看 explain 输出以查看 Mongo 如何实际处理这样的查询.

There's no difference between those conditions. The best way to answer questions like this is to look at explain output to take a look at how Mongo actually handles a query like this.

如果您查看下面查询计划中的 winingPlan,您会发现它们完全相同!仅仅因为 $and{a: 'aaa', b: 'bbb'} 中不显式并不意味着它不存在:它只是隐式的.

If you take a look at the winningPlan in the query-plans below, you can see that they're exactly the same! Just because the $and isn't explicit in {a: 'aaa', b: 'bbb'} doesn't mean it's not there: it's just implicit.

<代码>>db.my_test_coll.createIndex({a: 1, b: 1});//添加索引

没有$and:

> db.my_test_coll.find({a: 'aaa', b: 'bbb'}).explain()
{
    "queryPlanner" : {
        "plannerVersion" : 1,
        "namespace" : "test.my_test_coll",
        "indexFilterSet" : false,
        "parsedQuery" : {
            "$and" : [
                {
                    "a" : {
                        "$eq" : "aaa"
                    }
                },
                {
                    "b" : {
                        "$eq" : "bbb"
                    }
                }
            ]
        },
        "winningPlan" : {
            "stage" : "FETCH",
            "inputStage" : {
                "stage" : "IXSCAN",
                "keyPattern" : {
                    "a" : 1,
                    "b" : 1
                },
                "indexName" : "a_1_b_1",
                "isMultiKey" : false,
                "multiKeyPaths" : {
                    "a" : [ ],
                    "b" : [ ]
                },
                "isUnique" : false,
                "isSparse" : false,
                "isPartial" : false,
                "indexVersion" : 2,
                "direction" : "forward",
                "indexBounds" : {
                    "a" : [
                        "[\"aaa\", \"aaa\"]"
                    ],
                    "b" : [
                        "[\"bbb\", \"bbb\"]"
                    ]
                }
            }
        },
        "rejectedPlans" : [ ]
    },
    "ok" : 1
}

使用 $and:

> db.my_test_coll.find({$and: [{a: 'aaa'}, {b: 'bbb'}]}).explain()
{
    "queryPlanner" : {
        "plannerVersion" : 1,
        "namespace" : "test.my_test_coll",
        "indexFilterSet" : false,
        "parsedQuery" : {
            "$and" : [
                {
                    "a" : {
                        "$eq" : "aaa"
                    }
                },
                {
                    "b" : {
                        "$eq" : "bbb"
                    }
                }
            ]
        },
        "winningPlan" : {
            "stage" : "FETCH",
            "inputStage" : {
                "stage" : "IXSCAN",
                "keyPattern" : {
                    "a" : 1,
                    "b" : 1
                },
                "indexName" : "a_1_b_1",
                "isMultiKey" : false,
                "multiKeyPaths" : {
                    "a" : [ ],
                    "b" : [ ]
                },
                "isUnique" : false,
                "isSparse" : false,
                "isPartial" : false,
                "indexVersion" : 2,
                "direction" : "forward",
                "indexBounds" : {
                    "a" : [
                        "[\"aaa\", \"aaa\"]"
                    ],
                    "b" : [
                        "[\"bbb\", \"bbb\"]"
                    ]
                }
            }
        },
        "rejectedPlans" : [ ]
    },
    "ok" : 1
}

这篇关于Mongodb 条件与 $and 运算符和没有运算符的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆