Query Documents查询文档
On this page本页内容
Use the 使用MongoDB Shell中的db.collection.find()
method in the MongoDB Shell to query documents in a collection.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.查询筛选器参数确定选择条件。
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>
对。
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> }, ... }
To return all movies from the 要返回sample_mflix.movies
collection which are either rated PG
or PG-13
:sample_mflix.movies
集合中评级为PG
或PG-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")
Specify Logical Operators (AND
/ OR
)指定逻辑运算符(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
连接符连接复合查询的子句,以便查询选择集合中符合所有条件的文档。
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
连接起来,以便查询选择集合中至少匹配一个条件的文档。
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个奖项或具有genre
为genre
:
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:有关其他查询示例,请参阅: