What's New in Mongoose 5.10: Improved Transactions

What's New in Mongoose 5.10: Improved Transactions

The primary goal of a transaction is to let you update multiple documents in MongoDB in isolation, and potentially undo all the updates if some error occurs by aborting the transaction.

Mongoose 5.10.0 was released on August 14, 2020. Mongoose 5.10 is a semver minor version that introduces several important new features. In this article, I'll describe what is arguably the most important new feature: the [Connection#transaction()](https://mongoosejs.com/docs/api/connection.html#connection_Connection-transaction) function, which improves Mongoose's support for MongoDB transactions.

Why Mongoose Needs a Transaction Wrapper

The primary goal of a transaction is to let you update multiple documents in MongoDB in isolation, and potentially undo all the updates if some error occurs by aborting the transaction. For example, if you start a transaction and insert two documents, calling abortTransaction() makes it so neither of the documents end up in the database:

const session = await Customer.startSession();
session.startTransaction();

// Passing the `session` option is how you indicate that `create()` is part of a transaction
await Customer.create([{ name: 'Test' }], { session: session });
await Customer.create([{ name: 'Test2' }], { session: session });

await session.abortTransaction();

const count = await Customer.countDocuments();
count; // 0, no documents were inserted, because the transaction was aborted.

Sessions have a convenient withTransaction() function that handles aborting the transaction if an error occurs. For example, the below code shows that the withTransaction() function successfully undoes any changes when the transaction executor function throws an error.

data visualization

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Visual Analytics and Advanced Data Visualization

Visual Analytics and Advanced Data Visualization - How CanvasJS help enterprises in creating custom Interactive and Analytical Dashboards for advanced visual analytics for data visualization

Visualization Best Practices for Data Scientists

Visualization Best Practices for Data Scientists. Disclaimer: The ideas presented in this article are from the book: Story Telling With Data by Cole Nussbaumer Knaflic.

Applications Of Data Science On 3D Imagery Data

The agenda of the talk included an introduction to 3D data, its applications and case studies, 3D data alignment and more.

The Importance of Data Visualization

The Importance of Data Visualization - It is the process of converting raw data at hand into easy and understandable image-photo-graphics for fast, effective and accurate…

Data Quality Testing Skills Needed For Data Integration Projects

Data Quality Testing Skills Needed For Data Integration Projects. Data integration projects fail for many reasons. Risks can be mitigated when well-trained testers deliver support. Here are some recommended testing skills.