$query

Definition

  • $query

Deprecated since v3.2

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

The $query operator forces MongoDB to interpret an expressionas a query.

The following mongo operations are equivalent, andreturn only those documents in the collection named collection where theage field equals 25.

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

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

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

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

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

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

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

See also

For more information about queries in MongoDB seeQuery Documents,db.collection.find(), and Getting Started with MongoDB.

Note

Do not mix query forms. If you use the $queryformat, do not append cursor methods to thefind(). To modify the query use themeta-query operators,such as $explain.

Therefore, the following two operations are equivalent:

  1. db.collection.find( { $query: { age : 25 }, $explain: true } )
  2. db.collection.find( { age : 25 } ).explain()