翻译或纠错本页面

$query

On this page

Definition

$query

注解

Deprecated in the mongo Shell since v3.2
Starting in v3.2, the $query operator is deprecated in the mongo shell. In the mongo shell, use cursor methods instead.

The $query operator forces MongoDB to interpret an expression as a query.

The following mongo operations are equivalent, and return only those documents in the collection named collection where the age field equals 25.

db.collection.find( { $query: { age : 25 } } )
db.collection.find( { age : 25 } )

$query is necessary to work with documents that contain a field name query whose value is an embedded document, such as the following document:

{ _id: 1, age: 25, query: { a: 1 } }

The following find operation that does not use the $query operator will return no results:

db.documents.find( { query: { a: 1 } } )

To obtain the document, you will need to use the following query:

db.documents.find( { "$query": { query: { a: 1 } } } )

参见

For more information about queries in MongoDB see 查询文档, db.collection.find(), and Getting Started with MongoDB.

注解

Do not mix query forms. If you use the $query format, do not append cursor methods to the find(). 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()
←   $snapshot $natural  →