Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove _id in MongoDB and replace with another field as a Primary Key?

I'm have a huge documents in a collection. I want to remove auto generated Object Id (_id key) from all the documents and replace it with another field as a Primary key?

I don't understand is why is there a need for a default Object Id in first place?

like image 546
linthum Avatar asked Feb 22 '17 01:02

linthum


People also ask

Can we replace _id in MongoDB?

You cannot update it. You'll have to save the document using a new _id , and then remove the old document.

Is there something like primary key in MongoDB?

In MongoDB, _id field as the primary key for the collection so that each document can be uniquely identified in the collection. The _id field contains a unique ObjectID value. When you query the documents in a collection, you can see the ObjectId for each document in the collection.

How do I set primary key in MongoDB schema?

_id field is reserved for primary key in mongodb, and that should be a unique value. If you don't set anything to _id it will automatically fill it with "MongoDB Id Object". But you can put any unique info into that field.

Should I use _id in MongoDB?

You should NOT convert the ObjectId in the database to a string, and then compare it to another string. If you'd do this, MongoDB cannot use the _id index and it'll have to scan all the documents, resulting in poor query performance.


2 Answers

In mongodb each document must be unique, so you need an unique field to be used as id. If you do not provide one, mongodb will provide one for you automatically. However, if you want to give custom ids for whichever reason (improve query performance being one of them), you can do it manually. Here goes my suggestions:

If you are inserting a new document, you can manually set the _id field like:

doc._id = "12312312" //(Or whichever function to generate id you have)
doc.save(...)

But when you already have a document in the database, you cannot modify it anymore. What you can do is to make a copy of the document, save a new document with the same data and erase the old one:

// Fetch the documents
docs = db.clients.find({})

docs.forEach((doc) => {
   //For each field you copy the values
   new_doc.field = doc.field
   new_doc._id = //YOUR ID FUNCTION 

   // insert the document, using the new _id
   db.clients.insert(new_doc)

   // remove the document with the old _id
   db.clients.remove({_id: doc._id})
}

This question is similar to the following one:

How update the _id of one MongoDB Document?

Hope my answer was helpful

like image 181
Israel Zinc Avatar answered Oct 12 '22 10:10

Israel Zinc


It is not possible to remove the _id field.

From https://docs.mongodb.com/manual/core/document/#the-id-field:

In MongoDB, each document stored in a collection requires a unique _id field that acts as a primary key. If an inserted document omits the _id field, the MongoDB driver automatically generates an ObjectId for the _id field.

What you can do is name your primary key as _id.

like image 30
Douglas Henrique Avatar answered Oct 12 '22 10:10

Douglas Henrique