mongodb
duplicate documents
data integrity
database management
error prevention

How to stop insertion of Duplicate documents in a mongodb collection

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Duplicate documents in MongoDB collections can lead to data inconsistency and inefficient querying processes. Preventing the insertion of duplicates involves understanding MongoDB’s features, including indexing and unique constraints, as well as implementing application-level logic. This article provides technical explanations, examples, and strategies to ensure that duplicate documents are not inserted into your MongoDB collections.

Unique Indexes in MongoDB

MongoDB offers unique indexes, which enforce the uniqueness of the values within the index. By placing a unique constraint on a field or a combination of fields, you can prevent duplicate entries.

Creating a Unique Index

To create a unique index in a collection, you use the createIndex method with the unique option set to true. Here's an example:

javascript
db.collection.createIndex({ "fieldName": 1 }, { unique: true })

In this example, a unique index is created on fieldName. This means that no two documents in the collection can have the same value for fieldName.

Compound Unique Indexes

You can also create a compound unique index to enforce uniqueness on a combination of fields:

javascript
db.collection.createIndex({ "field1": 1, "field2": 1 }, { unique: true })

The above example ensures that the combination of field1 and field2 values is unique across the collection.

Application-Level Logic

Apart from enforcing unique indices, additional application-level logic can help mitigate the risk of duplicate entries.

Using upsert

MongoDB provides an upsert option in its update operations, allowing you to update an existing document or insert a new one if no document matches. This is useful to prevent duplicates based on specific criteria:

javascript
1db.collection.updateOne(
2  { "fieldName": "value" },
3  { $set: { "otherField": "newValue" } },
4  { upsert: true }
5)

This operation updates the document where fieldName is "value", or inserts a new document if none exists.

Pre-Insert Checks

Before inserting a document, perform a query to check for existing documents that match the uniqueness criteria. Here’s a simple example using JavaScript in a Node.js application:

javascript
1async function insertDocument(doc) {
2  const existingDoc = await db.collection.findOne({ "fieldName": doc.fieldName });
3  if (!existingDoc) {
4    await db.collection.insertOne(doc);
5  } else {
6    console.log("Duplicate detected. Document not inserted.");
7  }
8}

Handling Duplicates in Existing Collections

If your application already has duplicates, consider these strategies:

  1. Use Scripts for Cleanup: Write a script to identify and remove duplicate documents. This may involve deciding criteria for retaining the "master" document.
  2. Aggregation Framework: Use the aggregation framework to detect duplicates. For example, grouping by the criteria that determines uniqueness will help you locate duplicates.

Example Aggregation to Find Duplicates

javascript
1db.collection.aggregate([
2  { $group: { _id: "$fieldName", count: { $sum: 1 }, docs: { $push: "$_id" } } },
3  { $match: { count: { $gt: 1 } } }
4])

This aggregation groups documents by fieldName, counts them, and identifies where there are more than one document (count > 1).

Summary Table

StrategyDescription
Unique IndexEnforce unique constraints on specific fields or combinations of fields using MongoDB's indexing.
Compound Unique IndexEnsure combinations of field values in documents are unique.
upsert OperationUse in update operations to prevent duplicates when inserting new documents.
Pre-Insert ChecksImplement application-level logic to query the database before inserting a new document.
Cleanup ScriptsWrite scripts to programmatically identify and remove existing duplicates.
Use AggregationsLeverage aggregations to detect and handle duplicates within a collection.

These approaches provide a robust framework for preventing and managing duplicate documents, ensuring data integrity and reliability. Remember to tailor these strategies to fit the specific requirements and constraints of your application.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.