Docs HomeMongoDB Shell

Query Documents查询文档

Use the db.collection.find() method in the MongoDB Shell to query documents in a collection.使用MongoDB Shell中的db.collection.find()方法查询集合中的文档。

The examples on this page reference the Atlas sample dataset. 本页的示例引用了Atlas示例数据集You can create a free Atlas cluster and populate that cluster with sample data to follow along with these examples. 您可以创建一个免费的Atlas集群,并使用示例数据填充该集群,以配合这些示例。To learn more, see Get Started with Atlas.要了解更多信息,请参阅Atlas入门

Read All Documents in a Collection读取集合中的所有文档

To read all documents in the collection, pass an empty document as the query filter parameter to the find method. 若要读取集合中的所有文档,请将一个空文档作为查询筛选器参数传递给find方法。The query filter parameter determines the select criteria.查询筛选器参数确定选择条件。

Example

To return all documents from the sample_mflix.movies collection:要返回sample_mflix.movies集合中的所有文档,请执行以下操作:

use sample_mflix

db.movies.find()

This operation is equivalent to the following SQL statement:此操作等效于以下SQL语句:

SELECT * FROM movies

Specify Equality Condition指定相等条件

To select documents which match an equality condition, specify the condition as a <field>:<value> pair in the query filter document.要选择与相等条件匹配的文档,请在查询筛选文档中将条件指定为<field>:<value>对。

Example

To return all movies where the title equals Titanic from the sample_mflix.movies collection:要返回sample_mflix.movies集合中title等于Titanic的所有电影,请执行以下操作:

use sample_mflix

db.movies.find( { "title": "Titanic" } )

This operation corresponds to the following SQL statement:此操作对应于以下SQL语句:

SELECT * FROM movies WHERE title = "Titanic"

Specify Conditions Using Query Operators使用查询运算符指定条件

Use query operators in a query filter document to perform more complex comparisons and evaluations. 查询筛选文档中使用查询运算符可以执行更复杂的比较和评估。Query operators in a query filter document have the following form:查询筛选文档中的查询运算符具有以下形式:

{ <field1>: { <operator1>: <value1> }, ... }
Example

To return all movies from the sample_mflix.movies collection which are either rated PG or PG-13:要返回sample_mflix.movies集合中评级为PGPG-13的所有电影:

use sample_mflix

db.movies.find( { rated: { $in: [ "PG", "PG-13" ] } } )

This operation corresponds to the following SQL statement:此操作对应于以下SQL语句:

SELECT * FROM movies WHERE rated in ("PG", "PG-13")
Note

Although you can express this query using the $or operator, use the $in operator rather than the $or operator when performing equality checks on the same field.尽管可以使用$or运算符表达此查询,但在对同一字段执行相等性检查时,请使用$in运算符,而不是$or运算符。

Specify Logical Operators (AND / OR)指定逻辑运算符(AND/OR

A compound query can specify conditions for more than one field in the collection's documents. 复合查询可以为集合文档中的多个字段指定条件。Implicitly, a logical AND conjunction connects the clauses of a compound query so that the query selects the documents in the collection that match all the conditions.隐含地,逻辑AND连接符连接复合查询的子句,以便查询选择集合中符合所有条件的文档。

Example

To return movies which were released in Mexico and have an IMDB rating of at least 7:返回在墨西哥上映且IMDB评分至少为7的电影:

use sample_mflix

db.movies.find( { countries: "Mexico", "imdb.rating": { $gte: 7 } } )

Use the $or operator to specify a compound query that joins each clause with a logical OR conjunction so that the query selects the documents in the collection that match at least one condition.使用$or运算符可以指定一个复合查询,该查询将每个子句与逻辑OR连接起来,以便查询选择集合中至少匹配一个条件的文档。

Example

To return movies from the sample_mflix.movies collection which were released in 2010 and either won at least 5 awards or have a genre of Drama:要返回sample_mflix.movies系列中的电影,这些电影于2010年发布,至少获得了5个奖项或具有genregenre

use sample_mflix

db.movies.find( {
year: 2010,
$or: [ { "awards.wins": { $gte: 5 } }, { genres: "Drama" } ]
} )

Read Behavior读取行为

To learn more about the specific behavior of reading documents, see Behavior.要了解有关阅读文档的特定行为的详细信息,请参阅行为

Additional Query Tutorials其他查询教程

For additional query examples, see:有关其他查询示例,请参阅: