mongodb 的唯一 ID [英] Unique IDs with mongodb

查看:32
本文介绍了mongodb 的唯一 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我正在构建一个博客,我可以使用博客标题作为唯一标识符并通过 URL 解析它.但是,如果我想使用数字怎么办.你知道 twitter 如何拥有 www.twitter.com/username/statuses/9834542 吗?有没有人想出一个很好的方法来完成这项工作?使用_id"是不可能的,因为它太长了.

If I were building a blog I could use the blog title as the unique identifier and parse it through the URL. However, what if I wanted to use numbers. You know how twitter has www.twitter.com/username/statuses/9834542? Has anyone figured out a nice way of making this work? using "_id" is out of the question since it's way too long.

推荐答案

只要你能保证唯一性,你就不受限于使用默认的_id" MongoDB 供应.

As long as you can guarantee uniqueness, you're not constrained to using the default "_id" MongoDB supplies.

因此,如何生成此数字取决于您.如果您想将此数字存储在 MongoDB 中,那么您可以将其存储在一个单独的集合中,并为每个所需的新 URL 递增它.

Therefore, it's down to you how you generate this number. If you'd like to store this number inside MongoDB, then you could store it in a separate collection and increment it for every new URL required.

通过使用$inc动词,或者您可能想看看 MongoDB 如何自动更新或增加一个值.

Incrementing a field is achieved by using the $inc verb, or you may want to take a look at how MongoDB can atomically update or increment a value.

这篇关于mongodb 的唯一 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆