Advanced Features of MongoDB Every Developer Should Know

Toufiqur Rahman Tamkin - Sep 17 - - Dev Community

MongoDB, as one of the most popular NoSQL databases, is known for its flexibility and scalability. However, beyond the basic CRUD operations and indexing, MongoDB has several powerful, advanced features that expert developers can leverage to optimize performance, enhance security, and scale applications. In this blog, we'll dive into some of these advanced features and provide tips on how you can use them in your applications.

1. Sharding for Horizontal Scalability
MongoDB supports horizontal scaling through a process called sharding, which involves distributing data across multiple servers or clusters to handle large datasets and high-throughput operations.

How It Works:

  • A shard key is chosen to split data across multiple shards.
  • Mongos, the query router, directs queries to the appropriate shard based on the shard key.
  • Config servers store the metadata and manage the cluster.

Example:
Imagine an e-commerce app where users’ purchase data grows exponentially. You could shard the database based on user ID to ensure the database scales effectively.

sh.enableSharding("ecommerceDB")
sh.shardCollection("ecommerceDB.orders", { "userID": 1 })

Enter fullscreen mode Exit fullscreen mode

Choosing the right shard key is critical. The shard key should distribute the data evenly across shards to avoid hotspots.

2. Aggregation Framework for Advanced Data Processing
MongoDB's Aggregation Framework provides a way to process and transform data using a pipeline approach. This is especially useful for reporting, analytics, and data transformation tasks.

Example of Aggregation Pipeline:
Suppose you're building an analytics dashboard to calculate the average order value per customer over the last year. You can use the aggregation pipeline as follows:

db.orders.aggregate([
  { $match: { orderDate: { $gte: ISODate("2023-01-01"), $lte: ISODate("2023-12-31") } }},
  { $group: { _id: "$customerID", avgOrderValue: { $avg: "$orderTotal" } }},
  { $sort: { avgOrderValue: -1 }},
  { $limit: 10 }
])

Enter fullscreen mode Exit fullscreen mode

Key Operators:

  • $match: Filters documents by a condition (similar to WHERE in SQL).
  • $group: Groups documents by a key and applies aggregate functions like $sum, $avg, or $count.
  • $lookup: Enables joins between collections, adding a relational aspect to MongoDB.

The aggregation framework provides a flexible way to transform and analyze data efficiently, without needing to retrieve large datasets to process in the application.

3. Multi-Document ACID Transactions
Earlier versions of MongoDB offered atomicity only at the document level. However, with MongoDB 4.0 and above, you can perform multi-document ACID transactions, which ensure the integrity of data when multiple collections or documents need to be updated simultaneously.

Example:
Imagine a financial application where a fund transfer between two accounts is required. Both the debit and credit operations need to happen as a single atomic transaction.

const session = db.getMongo().startSession();
session.startTransaction();

try {
  db.accounts.updateOne(
    { _id: "A123" },
    { $inc: { balance: -100 } },
    { session }
  );

  db.accounts.updateOne(
    { _id: "B456" },
    { $inc: { balance: 100 } },
    { session }
  );

  session.commitTransaction();
} catch (error) {
  session.abortTransaction();
} finally {
  session.endSession();
}

Enter fullscreen mode Exit fullscreen mode

This transaction ensures that if any step fails (such as an insufficient balance), the entire operation is rolled back.

4. Schema Validation with JSON Schema
Even though MongoDB is schema-less, you can still enforce a structure on documents using JSON Schema Validation. This is particularly useful when working with large teams or microservices, ensuring that only valid documents are inserted into the collection.

Example:
For a user collection, you can enforce validation rules to ensure that every document contains the required fields, such as name and email, with correct data types.

db.createCollection("users", {
  validator: {
    $jsonSchema: {
      bsonType: "object",
      required: [ "name", "email" ],
      properties: {
        name: {
          bsonType: "string",
          description: "must be a string and is required"
        },
        email: {
          bsonType: "string",
          pattern: "^.+@.+$",
          description: "must be a valid email"
        }
      }
    }
  }
})

Enter fullscreen mode Exit fullscreen mode

This level of validation helps maintain data quality and prevents issues down the line caused by bad data.

5. TTL Indexes for Expiring Data
In applications where data is time-sensitive (e.g., session data or logs), you may not want to keep it indefinitely. MongoDB offers TTL (Time-to-Live) indexes, which automatically remove documents after a certain period.

Example:
If you store user session data in a collection and want to remove sessions after 24 hours, you can set up a TTL index:

db.sessions.createIndex({ createdAt: 1 }, { expireAfterSeconds: 86400 })

Enter fullscreen mode Exit fullscreen mode

This will automatically delete sessions 24 hours after their createdAt field is set. TTL indexes are particularly useful for maintaining data hygiene without manual intervention.

6. Change Streams for Real-Time Data
MongoDB's Change Streams allow you to monitor real-time changes in your collections and databases. This is especially useful for applications requiring real-time notifications, such as live data dashboards or syncing MongoDB with other systems.

Example:
For a chat application, you might want to listen to changes in the messages collection and notify users of new messages instantly.

const pipeline = [{ $match: { "operationType": "insert" } }];

const changeStream = db.collection("messages").watch(pipeline);

changeStream.on("change", (next) => {
  console.log("New message:", next.fullDocument);
});

Enter fullscreen mode Exit fullscreen mode

Change Streams can be filtered and aggregated, making them highly flexible for real-time applications that need to react to database updates.

7. Full-Text Search with MongoDB Atlas
While MongoDB natively supports simple text search using text indexes, MongoDB Atlas provides a more advanced full-text search capability built on Lucene. With Atlas, you can create sophisticated search indexes to support features like:

  • Autocomplete: For search-as-you-type functionality.
  • Relevance Scoring: To prioritize results based on custom scoring.
  • Faceting: To categorize results dynamically (e.g., price ranges, brands).

Example of Basic Text Search:

db.articles.createIndex({ content: "text" });

db.articles.find({
  $text: {
    $search: "mongodb scaling",
    $caseSensitive: false
  }
});

Enter fullscreen mode Exit fullscreen mode

For more advanced search features like fuzzy matching or custom scoring, MongoDB Atlas Search is a powerful tool that integrates seamlessly into your MongoDB instance.

Conclusion
MongoDB’s advanced features go beyond basic NoSQL storage, enabling developers to build highly scalable, performant, and reliable applications. From sharding and aggregation to ACID transactions and real-time change streams, MongoDB offers a rich feature set that every expert developer should explore.

By mastering these advanced capabilities, you can leverage MongoDB to handle complex use cases with ease and ensure that your applications remain robust even as they scale. Whether you're building large-scale systems, real-time applications, or data-driven services, MongoDB’s advanced features provide the tools you need to succeed in production environments.

. .
Terabox Video Player