I have a unique ID field of type int in my documents. I’m doing ensureIndex on this field, but my documents still contain _id. Can I get rid of it?
I have a unique ID field of type int in my documents. I’m doing
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
From the docs …
However, you can assign your own value to _id …
The better question is why would you create your own unique ID, build an index on it and remove the very useful ObjectID _id that is auto indexed?
All the official drivers use ObjectID and it has a lot of really good aspects to it:
It has a timestamp built in (so you don’t need to store a date field
in your document, and you can use it to order by date.)
It is a “global, uniformly increasing sequence number” — i.e.
it’ll still be unique across servers if you need to shard your data,
etc.