MongoDB create a collection inside a Transaction – Guidelines

MongoDB create a collection inside a Transaction

Today in this article we shall learn, how to create a collection inside a Transaction and a few guidelines around this feature, and a few known limitations.

MongoDB creates a collection inside a Transaction that is now supported with the 4.4 version.

MongoDB supports implicit DDL Operations which provide capabilities of implicitly creating a collection through the below-discussed listed operations against a non-existing collection.

That means MongoDB will create a missing collection if you perform any of the below operations once you have the right version of the MongoDB driver, you are all set to use this feature.

Today in this article, we will cover below aspects,

Collection can be created using any of the two below mechanisms,

  • Implicit Collection creation
  • Explicit Collection Creation

MongoDB 4.2 and earlier version – No Support

In the Transaction session – creating or dropping a collection or an index, are disallowed as they affect the database catalog.

MongoDB 4.4 and support for collection creation

This is one of the recent features added to MongoDB’s recent version 4.4. We can now perform the below operations within the transaction which was a limitation in the older version.

  • create a collection inside a transaction
  • drop a collection inside a transaction
  • create indexes inside the transaction
  • drop indexes inside the transaction

Inside of a multi-document transaction as long as the transaction is not a cross-shard write transaction, you perform any of the above operations.

Do you know few operations implicitly create Mongo Collection based on the type of operation you perform?

Yes… This implicit DDL feature is actually present for a long time but now will be supported for Transaction-based operations too.

Implicit Collection creation

We shall see all methods that are supported within transactions for implicit DDL in Mongo.

The above methods are related to C# language but the similar methods you shall find applicable to respective Mongo Driver equivalent methods used like Node.js or C# Mongo Driver etc.

Update method- supported for Transactions with Collection creation

Below are additional write methods in the form of update or replace which creates a collection if the option is used with the ‘Upsert’ flag as true.

  • db.collection.updateOne() with upsert: true

Example:

  • db.collection.updateMany() with upsert: true

Example:

            
            var collection = db.GetCollection<BsonDocument>("Book");
            var filter = Builders<BsonDocument>.Filter.Empty;
            var update = Builders<BsonDocument>.Update.Set("DateAdded", DateTime.Now);

            var options = new UpdateOptions { IsUpsert = true };
            collection.UpdateMany(filter, update, options);

  • db.collection.replaceOne() with upsert: true

Example:

       foreach (var rootObject in documents)
            {
                var filter = Builders<BsonDocument>.Filter.Eq("UserId", (ObjectId)rootObject.GetElement("UserId").Value);
                var options = new UpdateOptions { IsUpsert = true };
                collection.ReplaceOne(filter, rootObject,options);
            }

  • db.collection.update() with upsert: true

findAndModify, findOneAndReplace, findAndModify method – supported for Transaction with Collection creation

  • findAndModify operation with upsert: true
  • findOneAndReplace operation with upsert: true
  • findAndModify operation with upsert: true

Example

Insert Method – supported for Transactions with Collection creation

Below are the Insert Methods supported for Transaction with Collection creation,

  • db.collection.insertMany()

Example:

               var options = new InsertOneOptions { BypassDocumentValidation= false };
                collection.InsertOne(rootObject, options);

  • db.collection.insertOne()

Example

               var options = new InsertOneOptions { BypassDocumentValidation= false };
                collection.InsertOne(rootObject, options);

  • db.collection.insert()

Creating a collection explicitly in the Transaction

You can explicitly create a collection or indexes using the commands.

Example

  • db.createCollection()
  • db.collection.createIndex()
  • db.collection.createIndexes()

References :

You cannot create new collections in cross-shard write transactions.

Summary

Distributed transaction support in MongoDB operations is a great feature.

Now transactions can be used across multiple operations, collections, databases, documents, and shards.

Today we went over transactions and their support for collection for index creation within transaction scope for recent and older versions and known limitations.

Do you have any comments or ideas or any better suggestions to share?

Please sound off your comments below.

Happy Coding !!



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



Leave a Reply

Your email address will not be published. Required fields are marked *