For transactions:对于事务:
You can create collections and indexes in transactions. For details, see Create Collections and Indexes in a Transaction您可以在事务中创建集合和索引。有关详细信息,请参阅在事务中创建集合和索引The collections used in a transaction can be in different databases.事务中使用的集合可以在不同的数据库中。Note
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.您无法在跨分片写入事务中创建新集合。例如,如果你在一个分片中写入一个现有的集合,并在另一个分片中隐式创建一个集合,MongoDB无法在同一事务中执行这两个操作。You cannot write to capped collections.您不能写入封顶集合。You cannot use read concern从封顶集合中读取时,不能使用读取关注"snapshot"when reading from a capped collection. (Starting in MongoDB 5.0)"snapshot"。(从MongoDB 5.0开始)You cannot read/write to collections in the您无法对config,admin, orlocaldatabases.config、admin或local数据库中的集合进行读写操作。You cannot write to您无法写入system.*collections.system.*集合。You cannot return the supported operation's query plan using您无法使用explainor similar commands.explain或类似命令返回受支持操作的查询计划。
For cursors created outside of a transaction, you cannot call对于在事务外部创建的游标,您不能在事务内部调用getMoreinside the transaction.getMore。For cursors created in a transaction, you cannot call对于在事务中创建的游标,您不能在事务外部调用getMoreoutside the transaction.getMore。
You cannot specify the不能将killCursorscommand as the first operation in a transaction.killCursors命令指定为事务中的第一个操作。Additionally, if you run the此外,如果在事务中运行killCursorscommand within a transaction, the server immediately stops the specified cursors. It does not wait for the transaction to commit.killCursors命令,服务器会立即停止指定的游标。它不会等待事务提交。
Operations Supported in Multi-Document Transactions多文档事务中支持的操作
CRUD OperationsCRUD操作
The following read/write operations are allowed in transactions:事务中允许进行以下读/写操作:
| Note | ||
|---|---|---|
db.collection.aggregate() | aggregate |
|
db.collection.countDocuments() |
| |
db.collection.distinct() | distinct |
|
db.collection.find() | find | |
db.collection.deleteMany()db.collection.deleteOne()db.collection.remove() | delete | |
db.collection.findOneAndDelete()db.collection.findOneAndReplace()db.collection.findOneAndUpdate() | findAndModify |
|
db.collection.insertMany()db.collection.insertOne() | insert |
|
db.collection.updateOne()db.collection.updateMany()db.collection.replaceOne() | update |
|
db.collection.bulkWrite() |
|
Note
Updates to Shard Key Values分片键值更新
You can update a document's shard key value (unless the shard key field is the immutable 您可以通过在事务中或作为可重试写入发出单个文档_id field) by issuing single-document update / findAndModify operations either in a transaction or as a retryable write. update/findAndModify操作来更新文档的分片键值(除非分片键字段是不可变的_id字段)。For details, see Change a Document's Shard Key Value.有关详细信息,请参阅更改文档的分片键值。
Count Operation计数操作
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 provide a collection-level API 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来执行计数。
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来执行计数。
Distinct Operation独特的操作
To perform a distinct operation within a transaction:要在事务中执行不同的操作,请执行以下操作:
For unsharded collections, you can use the对于未记录的集合,您可以使用db.collection.distinct()method/thedistinctcommand as well as the aggregation pipeline with the$groupstage.db.collection.distinct()方法/distinct命令以及$group阶段的聚合管道。For sharded collections, you cannot use the对于分片集合,您不能使用db.collection.distinct()method or thedistinctcommand.db.collection.distinct()方法或distinct命令。To find the distinct values for a sharded collection, use the aggregation pipeline with the要查找分片集合的不同值,请使用带有$groupstage instead. For example:$group阶段的聚合管道。例如:Instead of用以下方式代替db.coll.distinct("x"), usedb.coll.distinct("x"):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.迭代游标以访问结果文档。
Administration Operations管理操作
You can create collections and indexes inside a distributed transaction if the transaction is not a cross-shard write transaction.如果分布式事务不是跨分片写入事务,则可以在该事务内创建集合和索引。
Explicit Create Operations显式创建操作
create | db.createCollection() | |
createIndexes | db.collection.createIndex()db.collection.createIndexes() |
Note
For more information on creating collections and indexes in a transaction, see Create Collections and Indexes in a Transaction.有关在事务中创建集合和索引的更多信息,请参阅在事务中新建集合和索引。
Implicit Create Operations隐式创建操作
You can also implicitly create a collection through the following write operations against a non-existing collection:您还可以通过以下对不存在的集合的写入操作隐式创建集合:
db.collection.findAndModify() with upsert: truedb.collection.findOneAndReplace() with upsert: truedb.collection.findOneAndUpdate() with upsert: true | findAndModify with upsert: true |
db.collection.insertMany()db.collection.insertOne() | insert |
db.collection.updateOne() with upsert: truedb.collection.updateMany() with upsert: truedb.collection.replaceOne() with upsert: true | update with upsert: true |
db.collection.bulkWrite()insert or upsert:true operationsinsert或upsert:true操作insert or upsert:true operationsinsert或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 Operations信息化运营
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(及其辅助方法);但是,它们不能是事务中的第一个操作。
Restricted Operations受限操作
The following operations are not allowed in transactions:事务中不允许执行以下操作:
Creating 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.在跨分片写入事务中创建新集合。例如,如果你在一个分片中写入一个现有的集合,并在另一个分片中隐式创建一个集合,MongoDB无法在同一事务中执行这两个操作。Explicit creation of collections, e.g.当使用db.createCollection()method, and indexes, e.g.db.collection.createIndexes()anddb.collection.createIndex()methods, when using a read concern level other than"local"."local"以外的读取关注级别时,显式创建集合,例如db.createCollection()方法,以及索引,例如db.collection.createIndexes()和db.collection.createIndex()方法。ThelistCollectionsandlistIndexescommands and their helper methods.listCollections和listIndexes命令及其辅助方法。Other non-CRUD and non-informational operations, such as其他非CRUD和非信息性操作,如createUser,getParameter,count, etc. and their helpers.createUser、createUser,getParameter、count等及其助手。Parallel operations. To update multiple namespaces concurrently, consider using the并行操作。要同时更新多个命名空间,请考虑使用bulkWritecommand instead.bulkWrite命令。