MongoDB: How to set your own _id field value (with examples)

Updated: February 3, 2024 By: Guest Contributor Post a comment

Introduction

MongoDB is a leading NoSQL database that offers flexibility, scalability, and rich features. One of its core concepts is the _id field, a unique identifier for documents within a collection. By default, MongoDB generates a unique ObjectId for each document, but it also allows users to set custom _id values. This tutorial provides a comprehensive guide on how to set your own _id field values in MongoDB, with practical examples ranging from basic to advanced.

Understanding the _id Field

The _id field is an essential part of every MongoDB document, acting as a primary key. Its default value is an ObjectId, consisting of 12 bytes that represent a timestamp, machine identifier, process ID, and a unique increment value. However, MongoDB allows the _id field to be of any type, including strings, numbers, or even composite types, as long as it remains unique within a collection.

Setting a Custom _id Value

Customizing the _id field can be beneficial for applications that require more semantic or easily understandable identifiers. To set a custom _id value, simply include the _id field in your document when inserting into a collection.

Example: Basic Insertion

db.collection.insertOne({
  _id: 'myCustomID123',
  name: 'John Doe',
  age: 30,
  occupation: 'Software Engineer'
});

This insertion creates a document with a custom _id of 'myCustomID123'. Remember to guarantee that the _id values you assign are unique to avoid insertion errors.

Example: Using Numbers as _id

db.collection.insertOne({
  _id: 101,
  name: 'Jane Doe',
  age: 29,
  occupation: 'Data Scientist'
});

This example demonstrates using a numerical value as the _id, allowing for numeric operations and ordering based on the _id.

Advanced Techniques

While setting simple custom _id values is straightforward, MongoDB also supports more complex scenarios. Here are some examples:

Composite _id Fields

For scenarios requiring compound identifiers, you can use embedded documents or arrays as your _id field value.

db.collection.insertOne({
  _id: { part1: 'partA', part2: 'partB' },
  name: 'Alice',
  age: 35,
  occupation: 'Architect'
});

This structure allows for unique composite keys, ideal for applications needing more elaborate identifiers.

Handling Duplicates

When setting custom _id values, it’s crucial to handle potential duplicates gracefully. Here’s how you can check for existing _id values before insertion:

if (!db.collection.findOne({_id: 'myCustomID123'})) {
  db.collection.insertOne({
    _id: 'myCustomID123',
    name: 'Bob',
    age: 27,
    occupation: 'Graphic Designer'
  });
} else {
  // Handle duplicate _id value
}

This approach ensures that you only insert a document if the _id value does not already exist, preventing errors.

Utilizing Bulk Operations

Bulk operations can be optimized by setting custom _id values in advance, thus reducing overhead and improving performance.

let bulkOp = db.collection.initializeUnorderedBulkOp();
bulkOp.insert({
  _id: 'uniqueID1',
  name: 'Dave',
  age: 32,
  occupation: 'Engineer'
});
bulkOp.insert({
  _id: 'uniqueID2',
  name: 'Eve',
  age: 28,
  occupation: 'Teacher'
});
// Execute the bulk operation
bulkOp.execute();

This method is especially useful for large-scale insertions or migrations.

Conclusion

Setting custom _id values in MongoDB offers significant advantages, from semantic identifiers to optimized performance. Whether you’re working with simple or complex data structures, understanding how to effectively manage the _id field is crucial for any MongoDB user. With the techniques outlined in this tutorial, you’re well on your way to leveraging custom _id values in your MongoDB applications.