Docs Home / mongosh / Perform CRUD Operations

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. You can create a free Atlas cluster and populate that cluster with sample data to follow along with these examples. To learn more, see Get Started with Atlas.本页上的示例引用了Atlas示例数据集。您可以创建一个免费的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. The query filter parameter determines the select criteria.要读取集合中的所有文档,请将一个空文档作为查询筛选器参数传递给find方法。查询筛选器参数确定选择条件。

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集合中标题等于《泰坦尼克号》的所有电影:

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$或$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:要返回2010年发布的sample_mflix.movies系列中至少获得5个奖项或具有戏剧类型的电影:

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:有关其他查询示例,请参阅: