How to provide _id for unique key and how to migrate existing records

Hey Team,
Current document we store in Mongo has got _id field automatically created. But what we want is we want to provide the id field from one of our microservice.
I went through some of blogs what I understood is we can provide hexadecimal string or int to ObjectId() and we can pass that id in document.
If we start providing _id while inserting new document will it work or not ??
Can you help me if I change the schema for the collection to take the user provided _id, will it be performance cost and any impact on existing data ??

Hello @Gheri_Rupchandani1 ,

Welcome back to MongoDB Community Forums! :wave:

It is not mandatory that _id can only be ObjectId(), the _id field may contain values of any BSON data type, other than an array, regex, or undefined.

Below blob is from the _id field documentation.

The following are common options for storing values for _id:

* Use an ObjectId.
* Use a natural unique identifier, if available. This saves space and avoids an additional index.
* Generate an auto-incrementing number.
* Generate a UUID in your application code. For a more efficient storage of the UUID values in the collection and in the _id index, store the UUID as a value of the BSON BinData type.
* Index keys that are of the BinData type are more efficiently stored in the index if:
       the binary subtype value is in the range of 0-7 or 128-135, and
       the length of the byte array is: 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, or 32.
*Use your driver's BSON UUID facility to generate UUIDs. Be aware that driver implementations may implement UUID serialization and deserialization logic differently, which may not be fully compatible with other drivers. See your driver documentation for information concerning UUID interoperability.

It should work. However, you need to provide an _id that does not exist yet in the collection, as it’s supposed to be the document’s primary key. Otherwise the insert will fail with an E11000 duplicate key error message. For more information, see the _id field .

As mentioned above, you need to make sure that an existing _id is not getting reinserted else it will give an error. Other than that, custom “_id” will not create any performance issues.

Note: Default _id Index

MongoDB creates a unique index on the _id field during the creation of a collection. The _id index prevents clients from inserting two documents with the same value for the _id field. You cannot drop this index on the _id field.

Regards,
Tarun

2 Likes

See a little example of custom _id from another thread:

2 Likes

I have two fields in collection in production env. How to create composite unique index ??
Please help me with both options
if its low volume traffic for some time in day i.e its okay for some performance degradation for some replicas??
high traffic all time i.e no degradation is allowed??

Also If I want to create one new field (x_field) in already exsiting collection if its going to impact performance ??

Thanks,
Gheri.

For creating a Unique Compound Index, kindly check

As the follow-up questions are not in-line with the original post, I would recommend you to open a new topic and add relevant details such as: MongoDB version, Example documents and any other details that might be helpful in understanding and answering your query.