Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate user friendly id's in MongoDb

Tags:

php

mysql

mongodb

There's this project I am working on. This is like a social network where we can have users, posts, pictures etc and then this problem came up. We are used to Mysql and the "almost magical" auto-increment field and now we cannot count on it anymore. I know the _id object in Mongo gives an easy way for identifying a document as it guarantee uniqueness. But the key is not user friendly and that's what we need, so we can make urls like:

http://website.com/posts/{post_id}
http://website.com/{user_id}

I developed a solution but I don't think this is the best way of doing this. I first create a mysql table with only one column. This column stores the user_id and it's an auto-increment field. For every new record on mongo I insert a new row in this mysql table and get the user_id with "LAST_INSERT_ID" function, now I can insert my data in my mongo collection with a numeric ID. And other benefit is that I can erase my mysql table let's say, after a million rows because the id's are already stored in mongo. Am I doing it wrong?

like image 454
Vinícius Barros Avatar asked Mar 03 '12 16:03

Vinícius Barros


People also ask

How does MongoDB generate unique IDs?

MongoDB uses ObjectIds as the default value of _id field of each document, which is generated during the creation of any document. Object ID is treated as the primary key within any MongoDB collection. It is a unique identifier for each document or record. Syntax: ObjectId(<hexadecimal>).

How do I get unique records in MongoDB?

In MongoDB, the distinct() method finds the distinct values for a given field across a single collection and returns the results in an array. It takes three parameters first one is the field for which to return distinct values and the others are optional.

Does MongoDB automatically assign ID?

MongoDB database drivers by default generate an ObjectID identifier that is assigned to the _id field of each document. In many cases the ObjectID may be used as a unique identifier in an application.

Do you need IDs in MongoDB?

All documents in MongoDB must have a populated _id field. If a document hasn't been assigned an _id value, MongoDB will automatically generate one.


3 Answers

Why not using slugs for posts and usernames for users? That should be human readable.

like image 78
dicarsio Avatar answered Oct 20 '22 20:10

dicarsio


First, I don't see any benefit to using an arbitrary auto incrementing number over the generated id mongo provides. Not only is not again just a arbitrary id, but you have to maintain the sequence.

That said, why not let mongo manage the id, and use another unique identifier for your URLs. If your users have a 'username', I'm assuming you've already made sure that's unique across the collection. Just query by that unique property, instead of finding by id.

That also allows the user to change their unique identifier, without you having to remap associations in the database.

And for the post, just generate a unique slug from the title.

like image 45
Tim Lytle Avatar answered Oct 20 '22 18:10

Tim Lytle


You can also create the id's in Mongo instead of MySQL, ...here's some documentation and articles on how to achieve it

http://www.mongodb.org/display/DOCS/How+to+Make+an+Auto+Incrementing+Field

http://shiflett.org/blog/2010/jul/auto-increment-with-mongodb

like image 30
raffian Avatar answered Oct 20 '22 18:10

raffian