MongoDb解释失败:“未知顶级运算符:$ query" [英] MongoDb explain failed: "unknown top level operator: $query"
问题描述
我正试图从非常简单的查询中获得解释.它使用具有以下架构的帖子集合:
I'm trying to obtain explain from quite simple query. It uses posts collection with following schema:
> db.posts.findOne()
{
"_id" : ObjectId("55236e6182bf196454a952b6"),
"Content" : "wuOfCjKborHcxkoyXzXiW",
"CreatedAtUtc" : ISODate("2014-01-18T23:59:30.023Z"),
"Tags" : [
"sjM",
"Van",
"Orm"
],
"Title" : "msAQAbQwAl",
"Author" : "yIIhato",
"Comments" : [ ]
}
我要解释的查询是这样:
Query I want to explain is this:
db.posts.find( { $query: {}, $orderby: { "CreatedAtUtc" : -1 } } )
它产生正确的结果,没有任何错误.但是在我要解释时会引发异常.我已经尝试过以下命令来解释查询:
It produces proper result without any errors. But it throws the exception when I want to explain it. I've tried these commands to explain query:
db.posts.explain().find( { $query: {}, $orderby: { "CreatedAtUtc" : -1 } } )
db.posts.find( { $query: {}, $orderby: { "CreatedAtUtc" : -1 } } ).explain()
var cursor = db.posts.find( { $query: {}, $orderby: { "CreatedAtUtc" : -1 } } )
cursor.explain()
错误始终相同:
2015-11-08T16:20:40.137+0100 E QUERY Error: explain failed: { "ok" : 0, "errm
sg" : "unknown top level operator: $query", "code" : 2 }
at Error (<anonymous>)
at Function.throwOrReturn (src/mongo/shell/explainable.js:34:19)
at constructor.finish (src/mongo/shell/explain_query.js:188:36)
at DBQuery.explain (src/mongo/shell/query.js:434:25)
at (shell):1:8 at src/mongo/shell/explainable.js:34
>
推荐答案
来自 文档 :
From the docs:
请勿混用查询表格.如果您使用
$query
格式,请不要附加 将光标方法移至find()
.要修改查询,请使用元查询 运算符,例如$explain
.
Do not mix query forms. If you use the
$query
format, do not append cursor methods to thefind()
. To modify the query use the meta-query operators, such as$explain
.
因此,以下两个操作是等效的:
Therefore, the following two operations are equivalent:
db.collection.find( { $query: { age : 25 }, $explain: true } )
db.collection.find( { age : 25 } ).explain()
因此,在您的情况下,作为 $explain
运算符自3.0版起已被弃用,请使用后一种查询形式,例如:
So in your case, as the $explain
operator has been deprecated since version 3.0, use the latter form of querying like:
db.posts.find({}).sort({ "CreatedAtUtc" : -1 }).explain();
这篇关于MongoDb解释失败:“未知顶级运算符:$ query"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!