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

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

问题描述

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

解决方案

只要可以保证唯一性,就不必使用默认的"_id" MongoDB提供的内容.

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

使用 $inc动词可以增加字段,或者您可以想看看MongoDB如何以原子方式更新或增加值. /p>

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.

解决方案

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

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.

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天全站免登陆