How to see the converted query by MongoDB shell
> db.foo.remove()
> db.foo.insert( { foo : "bar" } )
> var cursor = db.foo.find( {开发者_运维问答 foo : "bar" } ).sort({x : 1})
> cursor.hasNext()
true
> cursor.next()
{ "_id" : ObjectId("4e8ddace03998dbf81966015"), "foo" : "bar" }
> db.foo.find({ $query : {foo : "bar"}, $orderby : { x : 1}})
{ "_id" : ObjectId("4e8ddace03998dbf81966015"), "foo" : "bar" }
>
The following query:
var cursor = db.foo.find( { foo : "bar" } ).sort({x : 1})
has been converted to the following statement by the shell:
db.foo.find({ $query : {foo : "bar"}, $orderby : { x : 1}})
Question: Is there a way that I could see the converted query by the shell? In other words, given a query, can I see the converted form of query by the shell?
You can't see the exact query from the shell, but you can see exactly what the helper functions (sort
, size
, etc) do. Leaving off the parens will output what the JS query will execute:
> db.foo.find
function (query, fields, limit, skip) {
return new DBQuery(this._mongo, this._db, this, this._fullName, this._massageObject(query), fields, limit, skip);
}
> db.foo.find().sort
function (sortBy) {
return this._addSpecial("orderby", sortBy);
}
As you can see, all that sort
does is add the orderby
operator.
精彩评论