On this page本页内容
find
¶New in version 3.2.版本3.2中的新功能。
Executes a query and returns the first batch of results and the cursor id, from which the client can construct a cursor.执行查询并返回第一批结果和游标id,客户机可以从中构造游标。
Tip
Rather than run the 您可以使用find
command directly, you can use the db.collection.find()
helper provided in the mongo
shell or the equivalent helper in the drivers.mongo
shell中提供的db.collection.find()
帮助程序或驱动程序中的等效帮助程序,而不是直接运行find
命令。
Changed in version 4.4.在版本4.4中更改。MongoDB deprecates the MongoDB不推荐在oplogReplay
option to the find
command. find
命令中使用oplogReplay
选项。The optimization enabled by this flag in previous versions now happens automatically for eligible queries on the oplog. 在以前的版本中,此标志启用的优化现在会自动用于oplog上符合条件的查询。Therefore, you don’t need to specify this flag. 因此,不需要指定此标志。If specified, the server accepts the flag for backwards compatibility, but the flag has no effect.如果指定,服务器将接受向后兼容的标志,但该标志无效。
The find
command has the following syntax:find
语法如下所示:
The command accepts the following fields:该命令接受以下字段:
find |
string | |
filter |
document | |
document | ||
projection |
document |
|
hint |
string or document |
|
skip |
Positive integer | |
limit |
Non-negative integer | |
batchSize |
| |
singleBatch |
boolean | false 。 |
comment |
any |
Note
|
maxTimeMS |
positive integer |
Tip
|
readConcern |
document |
|
max |
document |
|
min |
document |
|
returnKey |
boolean | true ,则仅返回结果文档中的索引键。false 。find command does not use an index, the returned documents will be empty.true 且find 命令不使用索引,则返回的文档将为空。 |
showRecordId |
boolean | true ,则在返回的文档中添加字段$recordId。 |
tailable |
boolean | |
awaitData |
boolean | getMore command on the cursor temporarily if at the end of data rather than returning no data. getMore 命令,而不是不返回任何数据。find returns as normal.find 将正常返回。 |
oplogReplay |
boolean |
To use
Deprecated Changed in version 4.4.
|
noCursorTimeout |
boolean | |
allowPartialResults | boolean |
Starting in MongoDB 4.4, if If the queried shards are initially available for the |
collation |
document |
The collation option
|
boolean |
For more complete documentation on
|
The command returns a document that contains the cursor information, including the cursor id and the first batch of documents. 该命令返回一个包含游标信息的文档,包括游标id和第一批文档。For example, the following document is returned when run against a sharded collection:例如,在对碎片集合运行时,将返回以下文档:
cursor |
If the queried shards are initially available for the |
"ok" |
1 ) or failed (0 ).1 )还是失败(0 )。 |
In addition to the aforementioned 除了前面提到的find
-specific fields, the db.runCommand()
includes the following information for replica sets and sharded clusters:find
-专用字段外,db.runCommand()
还包括副本集和分片集群的以下信息:
$clusterTime
operationTime
See db.runCommand() Results for details.
New in version 4.0.版本4.0中的新功能。
For cursors created inside a session, you cannot call 对于在会话内创建的游标,不能在会话外调用getMore
outside the session.getMore
。
Similarly, for cursors created outside of a session, you cannot call 同样,对于在会话外部创建的游标,不能在会话内部调用getMore
inside a session.getMore
。
Starting in MongoDB 3.6, MongoDB drivers and the 从MongoDB 3.6开始,MongoDB驱动程序和mongo
shell associate all operations with a server session, with the exception of unacknowledged write operations. mongo
shell将所有操作与服务器会话相关联,但未确认的写入操作除外。For operations not explicitly associated with a session (i.e. using 对于未显式与会话关联的操作(即使用Mongo.startSession()
), MongoDB drivers and the mongo
shell creates an implicit session and associates it with the operation.Mongo.startSession()
),MongoDB驱动程序和mongo
shell会创建一个隐式会话,并将其与操作关联。
If a session is idle for longer than 30 minutes, the MongoDB server marks that session as expired and may close it at any time. When the MongoDB server closes the session, it also kills any in-progress operations and open cursors associated with the session. 如果会话空闲时间超过30分钟,MongoDB服务器会将该会话标记为已过期,并可随时将其关闭。当MongoDB服务器关闭会话时,它还会终止任何正在进行的操作以及与会话相关的打开游标。This includes cursors configured with 这包括配置了noCursorTimeout
or a maxTimeMS
greater than 30 minutes.noCursorTimeout
或maxTimeMS
(大于30分钟)的游标。
For operations that return a cursor, if the cursor may be idle for longer than 30 minutes, issue the operation within an explicit session using 对于返回游标的操作,如果游标空闲时间可能超过30分钟,请使用Session.startSession()
and periodically refresh the session using the refreshSessions
command. Session.startSession()
在显式会话中发出该操作,并使用refreshSessions
命令定期刷新会话。See 有关更多信息,请参阅会话空闲超时。Session Idle Timeout
for more information.
find
can be used inside multi-document transactions.可以在多文档事务中使用。
getMore
inside the transaction.getMore
。getMore
outside the transaction.getMore
。Important
In most cases, multi-document transaction incurs a greater performance cost over single document writes, and the availability of multi-document transactions should not be a replacement for effective schema design. 在大多数情况下,与单文档写入相比,多文档事务会带来更大的性能成本,而多文档事务的可用性不应取代有效的模式设计。For many scenarios, the denormalized data model (embedded documents and arrays) will continue to be optimal for your data and use cases. 对于许多场景,非规范化数据模型(嵌入式文档和数组)将继续适合您的数据和用例。That is, for many scenarios, modeling your data appropriately will minimize the need for multi-document transactions.也就是说,对于许多场景,适当地建模数据将最大限度地减少对多文档事务的需求。
For additional transactions usage considerations (such as runtime limit and oplog size limit), see also Production Considerations.有关其他事务使用注意事项(如运行时限制和oplog大小限制),请参阅生产注意事项。
The following command runs the 下面的命令在find
command filtering on the rating
field and the cuisine
field. rating
字段和cuisine
字段上运行find
命令筛选。The command includes a 该命令包含一个projection
to only return the following fields in the matching documents: _id
, name
, rating
, and address
fields.projection
,仅返回匹配文档中的以下字段:_id
、name
、rating
和address
字段。
The command sorts the documents in the result set by the 该命令按name
field and limits the result set to 5 documents.name
字段对结果集中的文档进行排序,并将结果集限制为5个文档。
To override the default read concern level of 要覆盖"local"
, use the readConcern
option."local"
的默认读取关注级别,请使用readConcern
选项。
The following operation on a replica set specifies a read concern of 以下对副本集的操作指定"majority"
to read the most recent copy of the data confirmed as having been written to a majority of the nodes."majority"
的读取关注点,以读取确认已写入多数节点的数据的最新副本。
To use read concern level of "majority"
, replica sets must use WiredTiger storage engine.
You can disable read concern "majority"
for a deployment with a three-member primary-secondary-arbiter (PSA) architecture; however, this has implications for change streams (in MongoDB 4.0 and earlier only) and transactions on sharded clusters. For more information, see Disable Read Concern Majority.有关更多信息,请参阅禁用读取关注多数。
Regardless of the read concern level, the most recent data on a node may not reflect the most recent version of the data in the system.无论读取关注级别如何,节点上的最新数据可能不会反映系统中数据的最新版本。
The getMore
command uses the readConcern
level specified in the originating find
command.
A readConcern
can be specified for the mongo
shell method db.collection.find()
using the cursor.readConcern
method:
For more information on available read concerns, see Read Concern.有关可用阅读关注点的更多信息,请参阅读关注点。
New in version 3.4.版本3.4中的新功能。
Collation排序规则 allows users to specify language-specific rules for string comparison, such as rules for lettercase and accent marks.允许用户为字符串比较指定特定于语言的规则,例如字母大小写和重音符号的规则。
The following operation runs the 以下操作使用指定的排序规则运行find
command with the collation specified:find
命令:
The mongo
shell provides the cursor.collation()
to specify collation for a db.collection.find()
operation.mongo
shell提供cursor.collation()
来为db.collection.find()
操作指定排序规则。
See also参阅