blog
MongoDB in 2018 – A Year in Review
MongoDB is by far the most popular choice in NoSQL world as its distributed architecture allows for more scalability and its document data model provides good flexibility to developers. Almost every year, major MongoDB version is released and 2018 is no exception. MongoDB 4.0 was released in July 2018, followed by some other minor releases as well. With MongoDB version 4.0, multi-document transactions and type conversions are supported now. MongoDB introduced new tool called MongoDB charts(beta) and added support of aggregation pipeline builder in MongoDB compass as well in 2018. In this article, we will go through some exciting features of MongoDB which were released in 2018.
Multi-Document ACID Transactions
This was the most awaited feature in MongoDB. Starting from version 4.0, multi-document acid transactions against replica sets are production ready and supported by MongoDB. All MongoDB transactions now extend ACID properties which ensures data integrity. It is really easy to add acid-transactions in any application which needs them and they don’t affect other operations which don’t require them. With support of multi-document acid transactions, any write operation which is performed inside the transaction, won’t be visible outside of the transaction. Here are some useful commands to add multi-document acid transactions inside your application.
Function | Description |
---|---|
Session.startTransaction() | Start a new transaction |
Session.commitTransaction() | Commits the transaction |
Session.abortTransaction() | Aborts the transaction |
Here is a small example of adding transaction operations using Mongo shell:
akashk:PRIMARY> use mydb
akashk:PRIMARY> db.createCollection(“newColl”)
akashk:PRIMARY> session = db.getMongo().startSession()
akashk:PRIMARY> session { "id" : UUID("62525323-1cd1-4ee8-853f-b78e593b46ba") }
akashk:PRIMARY> session.startTransaction()
akashk:PRIMARY> session.getDatabase("mydb").newColl.insert({name : 'hello'})
akashk:PRIMARY> WriteResult({ "nInserted" : 1 })
akashk:PRIMARY> session.commitTransaction()
All transactions provide consistent view of data across one or many collections in database using snapshot isolation. MongoDB won’t push any uncommitted changes to secondary nodes/replicas. Once a transaction is committed, all the changes will be applied to secondary nodes.
There are many examples where we can use MongoDB multi-document acid transactions such as,
- Funds transfer between bank accounts
- Payment system
- Trading system
- Supply chain system
- Billing system
Things to Consider While Adding Transactions
- MongoDB will abort any transaction which runs for more than 60 seconds.
- Not more than 1000 documents should be changed in a single transaction. No limit for read operations.
- Any transaction should be of size less than 16MB as MongoDb stores any transaction as a single entry in oplog.
- When you abort any transaction, all changes will be roll backed.
New Type Conversion Operators in Aggregation Pipeline
To get real-time insights of data and writing complex queries, MongoDB developers generally prefer to create aggregation pipeline. In MongoDB 4.0 version, some new aggregation type conversion operators have been added for querying data without cleansing of individual fields.
Aggregation operator | Description |
---|---|
$convert | Converts value to a specified type |
$toDate | Converts value to Date |
$toDecimal | Converts value to Decimal |
$toDouble | Converts value to Double |
$toLong | Converts value to Long |
$toInt | Converts value to Integer |
$toObjectId | Converts value to ObjectId |
$toString | Converts value to String |
$ltrim | Remove unnecessary characters from the beginning of the string |
$rtrim | Remove unnecessary characters from the end of the string |
$trim | Remove unnecessary characters from both sides of the string |
Extended Functionality of Change Streams
Functionality of change streams which provides real time data changes updates without any complex settings was introduced in version 3.6. With version 4.0, change streams can track changes of whole database or cluster instead of only a single collection now. Apart from this, now, change streams also returns cluster timestamp associated with an event which can be helpful for the server applications.
Faster Data Migrations
When your database is sharded across the cluster, adding and removing nodes elastically from a sharded cluster can be time consuming at some times. The sharded cluster balancer which is responsible to distribute data across all the shards, got major upgrade in version 4.0. Now, it can finish data migration at 40% faster rate.
Non-Blocking Secondary Reads
Previously, MongoDB used to block all the secondary reads when oplog entries were being applied to the secondary nodes. This was causing variable latency of secondary reads. From MongoDB 4.0, secondary reads have become non-blocking due to increased replica set throughput and improved read latencies.
Aggregation Pipeline Builder in Compass
MongoDB compass is the GUI tool for MongoDB to visualize and query data. This year, MongoDB compass got new feature of aggregation pipeline. It provides visual query editor for building multi stage aggregation pipelines. Here is the snapshot of it:
In addition to this feature, compass also has the ability to export your queries to any native code languages of your choice now.
MongoDB Charts
MongoDB Charts is the new tool which enables the user to quickly create real time visualizations of MongoDB data. This tool is built for document data model with support of type handling, array reductions and nested documents as well. This tool allows user to create chart dashboards and share it with other users. MongoDB charts is now fully integrated with MongoDB Atlas.
Other New MongoDB Features
- MongoDB Stitch: Serverless platform for client application development which can access Mongo services securely.
- MongoDB Kubernetes: For deploying MongoDB within Kubernetes cluster.
- MongoDB Mobile: Provides flexibility and power of MongoDB in a compact form so that it can be used in IOT devices.
- MongoDB Monitoring Cloud Service: To push monitoring metadata to MongoDB monitoring cloud for free.
The Future of MongoDB
MongoDB also plans to launch some new features with its version 4.2 which includes,
- More extensive WiredTiger engine
- Transaction manager
- Transactions across a sharded deployment
- Global point in time reads