Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working with ugly mongodb _ids on front end

A kinda subjective question but I have a few concerns about working with mongodb _ids on client side. I would better use something like s52ruf6wst or xR2ru286zjI for RESTful resources and working with small collections of items.

1) I'm starting to be dependent on proprietary implementation of backend database (_id field name and implementation). If I stick with this _ids it is harder to replace back-end db later.

2) I've got huge ugly URLs containing mongo _ids (even for REST endpoints - I don't like it)

3) For hackers and "curious users" it is become obvious which back-end db is used.

As I see most of web applications use their own conventions on how ids, uid, uuids should look like, and I would say to me it looks more professional (than using staightforward ugly implementation by db vendor).

So the question is when it is good to use standard mongo _ids and use them across back and front ends? And what can be done to improve the situation?

like image 526
WHITECOLOR Avatar asked Jan 11 '13 11:01

WHITECOLOR


2 Answers

when it is good to use standard mongo _ids

Always. Except when you simply don't like it. But your personal preferences have nothing to do with security. Mongo's object ids are not inherently less safe than any other identifier type (integer, UUID, etc.)

ObjectID is designed to be unique across your cluster and this is very important, because mongodb is a distributed DB. It also has a nice property: values are monotonically increasing with time. This property may or may not be useful to you.

like image 109
Sergio Tulentsev Avatar answered Nov 15 '22 02:11

Sergio Tulentsev


1) I'm starting to be dependent on proprietary implementation of backend database (_id field name and implementation). If I stick with this _ids it is harder to replace back-end db later.

This is where abstraction layers, frameworks and ODM (ORM)s come in. They provide a standardised layer (i.e. Doctrine 2) to query mutliple different types of database. As an exampe id translates in many ORMs as _id and ID and id depending on which database you are using.

As said before, the ObjectId has no inherent securiy flaws, it isn't even that useful in general to other users since even though the ObjectId has a time part this time part cannot be easily used to decide what the next object is (unlike an auto incrementing ID). The only way to do this reliably would be to test all times up until now and all pid numbers to detect if a hidden object exists. So it is not very easy at all to crawl ObjectId URLs and in fact are not very SEO friendly for that exact reason. But yes they could know what database you are using.

That being said, yes they are ugly but they are that long and ugly to be, as @Sergio says, unique. Making your own will be just as bad. I suppose you could shrink it a little by base64 encoding the hexadecimal representation of the ObjectId.

However I am unsure if you really need that.

like image 22
Sammaye Avatar answered Nov 15 '22 00:11

Sammaye