Open In App

How to Update the _id of MongoDB Document?

In MongoDB, the _id field serves as a unique identifier for each document in a collection. While MongoDB automatically generates _id values for documents upon insertion, there are scenarios where we might need to update the _id of a document. In this article, We will learn about How to update the _id of one MongoDB Document with the help of examples and so on.

How to Update the _id of One MongoDB Document?

When working with MongoDB, there might be situations where we need to update the _id of a document. This can be challenging, as MongoDB does not provide a direct method to update the _id field. However, by following a few key steps, we can achieve this task. We cant update the _id of one MongoDB Document directly we need to follow some steps:



  1. Create a document with an _id.
  2. Retrieve the document with existing _id.
  3. Modify the _id value to the desired new value.
  4. Insert the modified document with new _id.
  5. Optionally, remove the original document with old _id.

To understand How to update the _id of one MongoDB Document we need a table on which we will perform various operations and queries. Let’s consider a scenario where we have a collection named “geeks_for_geeks_collection” containing documents representing articles. We’ll update the _id of one of these documents.

1. Create a Document with an _id

We begin by inserting a document with the original _id value of 1 into the “geeks_for_geeks_collection“.



// Original document with the existing _id
print("Original document:")
db.geeks_for_geeks_collection.insertOne({_id: 1, title: "Introduction to MongoDB", author: "GeekyAuthor1"})
printjson(db.geeks_for_geeks_collection.findOne({_id: 1}))

Output:

Inserting a document

2. Retrieve the Document with Existing _id

Next, we retrieve this document using the findOne() method and store it in the originalDocument variable.

// Retrieve the document with the existing _id
print("\nRetrieving original document...")
var originalDocument = db.geeks_for_geeks_collection.findOne({_id: 1})
print("Original document:")
printjson(originalDocument)

Output:

Retrieve the document

3. Modify the _id Value to the Desired New Value

We then modify the _id value of originalDocument to the desired new value, in this case, 1001.

// Modify the _id value to the desired new value
print("\nModifying _id value...")
originalDocument._id = 1001
print("Modified document:")
printjson(originalDocument)

Output:

Modify the _id

4. Insert the Modified Document with New _id

Subsequently, we insert the modified document with the new _id into the collection.

// Insert the modified document with the new _id
print("\nInserting modified document with new _id...")
db.geeks_for_geeks_collection.insertOne(originalDocument)
print("Collection after insertion:")
db.geeks_for_geeks_collection.find().forEach(printjson)

Output:

Insert the modified document

5. Optionally, Remove the Original Document with Old _id

Finally, we optionally remove the original document with the old _id using the deleteOne() method.

// Optionally, remove the original document with the old _id
print("\nRemoving original document...")
db.geeks_for_geeks_collection.deleteOne({_id: 1})
print("Collection after deletion:")
db.geeks_for_geeks_collection.find().forEach(printjson)

Output:

Optionally remove the original document

Conclusion

Overall, updating the _id of a MongoDB document involves a series of steps, as MongoDB does not provide a direct method to update the _id field. By creating a new document with the desired _id retrieving the existing document modifying the _id value and then inserting the modified document, you can effectively update the _id of a MongoDB document. Additionally, you have the option to remove the original document with the old _id if needed. Understanding these steps and their implementation can help you manage and manipulate data more effectively in MongoDB.

Article Tags :