6

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?

2
  • 3
    You expect users to remember an integer? The only person I know who gets away with that is Randall, of xkcd fame, and he only has just over 1000. Just use the mongo IDs- the user's are not memorizing them anyway. Mar 3, 2012 at 16:58
  • 2
    "user friendly" stands for human readable and understandable content. I don't need users to memorize it, but I need it for other reasons. Mar 3, 2012 at 17:22

3 Answers 3

8

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

1
  • 3
    Definition from Wikipedia: "A slug is the part of a URL which identifies a page using human-readable keywords [1][2], for example "Slug_(web_publishing)", rather than an opaque identifier such as the ID number of the content within the database (e.g. "27077911"). Slugs are used to construct clean URLs (often for permalinks) that are easy to type, descriptive, and easy to remember. For example, in this URL: example.org/2011/introduction-to-blogging the slug is "introduction-to-blogging"."
    – dicarsio
    Mar 6, 2012 at 17:44
6

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.

5

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.