在MongoDb中生成用户友好的id

时间:2022-12-05 12:55:28

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:

这是我正在做的一个项目。这就像一个社交网络,我们可以有用户,帖子,图片等等,然后这个问题就出现了。我们已经习惯了Mysql和“几乎不可思议”的自动增量字段,现在我们再也不能指望它了。我知道Mongo中的_id对象为标识文档提供了一种简单的方法,因为它保证了惟一性。但关键不是用户友好,这是我们需要的,所以我们可以做url:

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?

我提出了一个解决方案,但我不认为这是最好的办法。我首先创建一个只有一列的mysql表。此列存储user_id,它是一个自动递增字段。为每个新纪录在mongo我这个mysql表中插入一个新行,user_id“LAST_INSERT_ID”功能,现在我可以插入我的数据在我mongo集合与数字ID。和其他好处是我可以擦掉mysql表假设,在一百万行,因为ID已经存储在mongo。我做错了吗?

3 个解决方案

#1


5  

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

您还可以在Mongo中创建id,而不是MySQL……这里有一些关于如何实现它的文档和文章

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

http://www.mongodb.org/display/DOCS/How + + + +汽车+递增+字段

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

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

#2


8  

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

为什么不为文章使用鼻涕虫,为用户使用用户名?这应该是人类可读的。

#3


7  

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.

首先,我认为在mongo提供的生成的id上使用任意的自动递增数字没有任何好处。不仅是一个任意的id,还必须维护序列。

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.

也就是说,为什么不让mongo管理id,并为url使用另一个唯一标识符呢?如果您的用户有一个“用户名”,我假设您已经确定了在整个集合中是唯一的。只需通过该唯一属性进行查询,而不是通过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.

对于这个帖子,只需要从标题中生成一个独特的slug。

#1


5  

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

您还可以在Mongo中创建id,而不是MySQL……这里有一些关于如何实现它的文档和文章

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

http://www.mongodb.org/display/DOCS/How + + + +汽车+递增+字段

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

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

#2


8  

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

为什么不为文章使用鼻涕虫,为用户使用用户名?这应该是人类可读的。

#3


7  

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.

首先,我认为在mongo提供的生成的id上使用任意的自动递增数字没有任何好处。不仅是一个任意的id,还必须维护序列。

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.

也就是说,为什么不让mongo管理id,并为url使用另一个唯一标识符呢?如果您的用户有一个“用户名”,我假设您已经确定了在整个集合中是唯一的。只需通过该唯一属性进行查询,而不是通过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.

对于这个帖子,只需要从标题中生成一个独特的slug。