On this page本页内容
db.collection.
findOne
(query, projection)¶mongo
Shell Method方法
This page documents the 本页记录了mongo
shell method, and does not refer to the MongoDB Node.js driver (or any other driver) method. mongo
shell方法,未提及MongoDB Node.js驱动程序(或任何其他驱动程序)方法。For corresponding MongoDB driver API, refer to your specific MongoDB driver documentation instead.有关相应的MongoDB驱动程序API,请参阅特定的MongoDB驱动程序文档。
Returns one document that satisfies the specified query criteria on the collection or view. 返回一个满足集合或视图上指定查询条件的文档。If multiple documents satisfy the query, this method returns the first document according to the natural order which reflects the order of documents on the disk. 如果多个文档满足查询,该方法将根据反映磁盘上文档顺序的自然顺序返回第一个文档。In capped collections, natural order is the same as insertion order. If no document satisfies the query, the method returns null.在封顶集合中,自然顺序与插入顺序相同。如果没有满足查询的文档,该方法将返回null
。
query |
document | |
projection |
document |
Returns: | projection parameter, findOne() returns a document that only contains the projection fields. projection 参数,findOne() 将返回仅包含projection 字段的文档。_id field is always included unless you explicitly exclude it._id 字段,否则它始终包含在内。
|
---|
Starting in MongoDB 4.2, if the client that issued the 从MongoDB 4.2开始,如果发出db.collection.findOne()
disconnects before the operation completes, MongoDB marks the db.collection.findOne()
for termination (i.e. killOp
on the operation).db.collection.findOne()
的客户端在操作完成之前断开连接,MongoDB会将db.collection.findOne()
标记为终止(即,操作上的killOp
)。
Language Consistency语言一致性
Starting in MongoDB 4.4, as part of making 从MongoDB 4.4开始,作为使find
and findAndModify
projection consistent with aggregation’s $project
stage,find
和findAndModify
投影与聚合的$project
阶段保持一致的一部分,
find
and findAndModify
projection can accept aggregation expressions and syntax.find
和findAndModify
投影可以接受聚合表达式和语法。Projection Restrictions
for details.The projection
parameter determines which fields are returned in the matching documents. projection
参数确定匹配文档中返回的字段。The projection
parameter takes a document of the following form:projection
参数采用以下形式的文档:
<field>: <1 or true> |
|
<field>: <0 or false> |
|
"<field>.$": <1 or true> |
$ array projection operator, you can specify the projection to return the first element that match the query condition on the array field; e.g. "arrayField.$" : 1 . $ 数组投影运算符,可以指定投影以返回与数组字段上的查询条件匹配的第一个元素;例如,"arrayField.$" : 1 。 |
<field>: <array projection> |
$elemMatch , $slice , specifies the array element(s) to include, thereby excluding those elements that do not meet the expressions. $elemMatch 、$slice ,指定要包含的数组元素,从而排除那些不符合表达式的元素。 |
<field>: <$meta expression> |
$meta operator expression, specifies the inclusion of available per-document metadata . $meta 运算符表达式,指定包含每个文档可用的元数据。 |
<field>: <aggregation expression> |
|
For fields in an embedded documents, you can specify the field using either:对于嵌入文档中的字段,可以使用以下任一方法指定字段:
"field.nestedfield": <value>
{ field: { nestedfield: <value> } }
(Starting in MongoDB 4.4)_id
The 默认情况下,_id
field is included in the returned documents by default unless you explicitly specify _id: 0
in the projection to suppress the field._id
字段包含在返回的文档中,除非在投影中明确指定_id: 0
以抑制该字段。
A projection
cannot contain both include and exclude specifications, with the exception of the _id
field:projection
不能同时包含包含和排除规范,但_id
字段除外:
_id
field is the only field that you can explicitly exclude._id
字段是唯一可以显式排除的字段。_id
field is the only field that you can explicitly include; however, the _id
field is included by default._id
字段是唯一可以显式包括的字段;但是,默认情况下会包含_id
字段。For more information on projection, see also:有关投影的更多信息,请参阅:
The following operation returns a single document from the bios collection:以下操作从bios
集合返回单个文档:
The following operation returns the first matching document from the bios collection where either the field 以下操作返回first
in the embedded document name
starts with the letter G
or where the field birth
is less than new Date('01/01/1945')
:bios
集合中的第一个匹配文档,其中嵌入文档name
中的first
字段以字母G
开头,或者字段birth
小于new Date('01/01/1945')
:
The projection
parameter specifies which fields to return. projection
参数指定要返回的字段。The parameter contains either include or exclude specifications, not both, unless the exclude is for the 参数包含包含或排除规范,而不是两者,除非排除是针对_id
field._id
字段的。
The following operation finds a document in the bios collection and returns only the 以下操作在name
, contribs
and _id
fields:bios
集合中查找文档,并仅返回name
、contribs
和_id
字段:
The following operation returns a document in the bios collection where the 以下操作将返回contribs
field contains the element OOP
and returns all fields except the _id
field, the first
field in the name
embedded document, and the birth
field:bios
集合中的一个文档,其中contribs
字段包含元素OOP
,并返回除_id
字段、name
嵌入文档中的first
字段和birth
字段之外的所有字段: