哪一种是在mongo shell上获取文档的最佳方式

时间:2015-10-21 20:54:11

标签: mongodb mongodb-query

1> db.people.find({$ and:[{name:{$ gt:" D"}},{name:{$ regex:" a"}}]});

2 - ; db.people.find({name:{$ gt:" D",$ regex:" a"}});

MongoDB查询都在mongo shell中提供相同的文档。 我想知道哪一个是OPTIMAL,为什么?

1 个答案:

答案 0 :(得分:0)

两者都应该是一样的。如果您对第二个查询运行说明,则可以看到以下内容和正在使用的$and运算符

> db.people.find( { name : { $gt : "D", $regex : "a" } } ).explain()
{
        "queryPlanner" : {
                "plannerVersion" : 1,
                "namespace" : "test.people",
                "indexFilterSet" : false,
                "parsedQuery" : {
                        "$and" : [
                                {
                                        "name" : {
                                                "$gt" : "D"
                                        }
                                },
                                {
                                        "name" : /a/
                                }
                        ]
                },
相关问题