Edy Bourne
Edy Bourne

Reputation: 6187

Mongodb / morphia and a RESTful APIs - how to handle ids?

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?

Thanks!!

Upvotes: 0

Views: 177

Answers (2)

evanchooly
evanchooly

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

xeraa
xeraa

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

Related Questions