On this page
$query
在本页面
Definition
$query
Note
从 v3.2 开始在
mongo
Shell 中弃用从 v3.2 开始,$query运算符在mongo shell 中已弃用。在mongoShell 中,改用cursor methods。
$query运算符强制 MongoDB 将表达式解释为查询。
以下mongo操作是等效的,并且仅返回age
字段等于25
的名为collection
的集合中的那些文档。
db.collection.find( { $query: { age : 25 } } )
db.collection.find( { age : 25 } )
必须使用$query来处理包含字段名称query
的文档,其值是嵌入式文档,例如以下文档:
{ _id: 1, age: 25, query: { a: 1 } }
以下不使用$query运算符的查找操作将不返回任何结果:
db.documents.find( { query: { a: 1 } } )
要获取文档,您将需要使用以下查询:
db.documents.find( { "$query": { query: { a: 1 } } } )
See also
有关 MongoDB 中查询的更多信息,请参见Query Documents,db.collection.find()和MongoDB 入门。
Note
不要混用查询表格。如果您使用$query格式,请勿将cursor methods附加到find()。要修改查询,请使用meta-query operators,例如$explain。
因此,以下两个操作是等效的:
db.collection.find( { $query: { age : 25 }, $explain: true } )
db.collection.find( { age : 25 } ).explain()