Reputation: 6187
Mongodb recommends having an ObjectId
id for every persisted document, but that doesn't go very well with RESTful APIs where urls often include short, easy to remember ids like /users/12/about
or /projects/1/users
and so forth.
What are the best practices to dealing with this?
_id
be a long number and use a counters collection to keep track of them?_id
alone and creating a separate field for those sequential numbers?Thanks!!
Upvotes: 0
Views: 177
Reputation: 6233
You can use whatever you'd like for your IDs. If you don't let mongodb assign an ObjectId on insertion, you'd just have to manage those IDs and guarantee their uniqueness yourself. This is not a problem but it can take a bit of work to make sure it's right. But if that's what you need, there's absolutely nothing wrong with doing so.
Upvotes: 1
Reputation: 10859
I wouldn't change the _id
if you want to use it for @Reference
. (At least historically) this has led to issues with references.
A separate id field is probably a better solution. Or (if available) use a natural ID. For example if there's a unique username, use that instead of a numeric ID.
Upvotes: 1