On this page本页内容
For transactions:对于事务:
The collections used in a transaction can be in different databases.事务中使用的集合可以位于不同的数据库中。
You cannot create new collections in cross-shard write transactions. 不能在跨分片写入事务中创建新集合。For example, if you write to an existing collection in one shard and implicitly create a collection in a different shard, MongoDB cannot perform both operations in the same transaction.例如,如果在一个shard中写入现有集合,并在另一个shard中隐式创建集合,则MongoDB无法在同一事务中执行这两个操作。
"snapshot"
when reading from a capped collection. (Starting in MongoDB 5.0)"snapshot"
。(从MongoDB 5.0开始)config
, admin
, or local
databases.config
、admin
或local
数据库中的集合。system.*
collections.system.*
集合。explain
).explain
)。getMore
inside the transaction.getMore
。getMore
outside the transaction.getMore
。killCursors
as the first operation in a transaction.killCursors
指定为事务中的第一个操作。Operations that affect the database catalog, such as creating or dropping a collection or an index, are not allowed in multi-document transactions. 在多文档事务中,不允许执行影响数据库目录的操作,例如创建或删除集合或索引。For example, a multi-document transaction cannot include an insert operation that would result in the creation of a new collection. 例如,多文档事务不能包含导致创建新集合的插入操作。See Restricted Operations.请参阅受限操作。
The following read/write operations are allowed in transactions:事务中允许以下读/写操作:
db.collection.aggregate() | aggregate |
|
db.collection.countDocuments() |
| |
db.collection.distinct() | distinct |
$group stage. $group 阶段。 |
db.collection.find() | find | |
delete | ||
findAndModify |
| |
insert |
| |
update |
| |
|
Starting in MongoDB 4.2, you can update a document's shard key value (unless the shard key field is the immutable 从MongoDB 4.2开始,您可以通过在事务或可重试写入中发出单个文档更新/查找和修改操作来更新文档的shard键值(除非分片键字段是不可变的_id
field) by issuing single-document update/findAndModify operations either in a transaction or as a retryable write. _id
字段)。For details, see Change a Document's Shard Key Value.有关详细信息,请参阅更改文档的Shard键值。
To perform a count operation within a transaction, use the 要在事务中执行计数操作,请使用$count
aggregation stage or the $group
(with a $sum
expression) aggregation stage.$count
聚合阶段或$group
(带有$sum
表达式)聚合阶段。
MongoDB drivers compatible with the 4.0 features provide a collection-level API 与4.0功能兼容的MongoDB驱动程序提供了一个集合级API countDocuments(filter, options)
as a helper method that uses the $group
with a $sum
expression to perform a count. countDocuments(filter, options)
作为辅助方法,使用带有$sum
表达式的$group
来执行计数。The 4.0 drivers have deprecated the 4.0驱动程序已经弃用了count()
API.count()
API。
Starting in MongoDB 4.0.3, 从MongoDB 4.0.3开始,mongosh
provides the db.collection.countDocuments()
helper method that uses the $group
with a $sum
expression to perform a count.mongosh
提供了db.collection.countDocuments()
辅助方法,该方法使用带有$sum
表达式的$group
来执行计数。
To perform a distinct operation within a transaction:要在事务中执行不同的操作:
db.collection.distinct()
method/the distinct
command as well as the aggregation pipeline with the $group
stage.db.collection.distinct()
方法/distinct
命令以及带有$group
阶段的聚合管道。For sharded collections, you cannot use the 对于分片集合,不能使用db.collection.distinct()
method or the distinct
command.db.collection.distinct()
方法或distinct
命令。
To find the distinct values for a sharded collection, use the aggregation pipeline with the 要查找分片集合的不同值,请将聚合管道与$group
stage instead. $group
阶段一起使用。For example:例如:
Instead of db.coll.distinct("x")
, use
db.coll.aggregate([ { $group: { _id: null, distinctValues: { $addToSet: "$x" } } }, { $project: { _id: 0 } } ])
Instead of 不要使用db.coll.distinct("x", { status: "A" })
, use:db.coll.distinct("x", { status: "A" })
,请使用:
db.coll.aggregate([ { $match: { status: "A" } }, { $group: { _id: null, distinctValues: { $addToSet: "$x" } } }, { $project: { _id: 0 } } ])
The pipeline returns a cursor to a document:管道将游标返回到文档:
{ "distinctValues" : [ 2, 3, 1 ] }
Iterate the cursor to access the results document.迭代游标以访问结果文档。
Starting in MongoDB 4.4 with feature compatibility version (fcv) 从功能兼容版本(fcv)"4.4"
, you can create collections and indexes inside a multi-document transaction if the transaction is not a cross-shard write transaction."4.4"
的MongoDB 4.4开始,如果事务不是跨分片写入事务,则可以在多文档事务内创建集合和索引。
Command | Notes | |
---|---|---|
create | db.createCollection() | |
createIndexes |
For more information on creating collections and indexes in a transaction, see Create Collections and Indexes In a Transaction.有关在事务中创建集合和索引的更多信息,请参阅在事务中创建集合和索引。
You can also implicitly create a collection through the following write operations against a non-existing collection:您还可以对不存在的集合通过以下写入操作隐式创建集合:
findAndModify upsert: true | |
insert | |
update upsert: true | |
For other CRUD operations allowed in transactions, see CRUD Operations.有关事务中允许的其他CRUD操作,请参阅CRUD操作。
For more information on creating collections and indexes in a transaction, see Create Collections and Indexes In a Transaction.有关在事务中创建集合和索引的更多信息,请参阅在事务中创建集合和索引。
Informational commands, such as 事务中允许使用信息命令,如hello
, buildInfo
, connectionStatus
(and their helper methods) are allowed in transactions; however, they cannot be the first operation in the transaction.hello
、buildInfo
、connectionStatus
(及其助手方法);但是,它们不能是事务中的第一个操作。
Changed in version 4.4.在版本4.4中更改。
The following operations are not allowed in transactions:事务中不允许以下操作:
db.createCollection()
method, and indexes, e.g. db.collection.createIndexes()
and db.collection.createIndex()
methods, when using a read concern level other than "local"
.db.createCollection()
方法,以及索引,例如db.collection.createIndexes()
和db.collection.createIndex()
方法,当使用非"local"
的读取关注级别时。listCollections
and listIndexes
commands and their helper methods.listCollections
和listIndexes
命令及其助手方法。createUser
, getParameter
, count
, etc. and their helpers.createUser
、getParameter
、count
等及其助手。