如何缩短mongo ID以更好地在URL中使用? [英] How can one shorten mongo ids for better use in URLs?

查看:119
本文介绍了如何缩短mongo ID以更好地在URL中使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经建立了许多使用mongodb作为数据库后端的python驱动站点,并且对它的ObjectId系统非常满意,但是,我希望能够以较短的方式编码id,而无需构建映射集合或利用网址缩短程序服务.

I've built a number of python driven sites that utilize mongodb as a database backend and am very happy with it's ObjectId system, however, I'd love to be able encode the ids in a shorter fashion without building a mapping collection or utilizing a url-shortener service.

建议?成功的故事吗?

推荐答案

您可以将它们压缩为Base62数字.这样不会节省很多空间,但是每个ID确实可以为您节省几个字节.我的示例是Ruby,但是在Python中使用类似的方法并不难.

You could compress them as Base62 numbers. This doesn't save a lot of space, but it does save you a few bytes per ID. My example's Ruby, but similar usage in Python wouldn't be hard.

ree-1.8.7-2010.02 > User.first._id.to_s
 => "4c76f3dd98db74697500003b"

ree-1.8.7-2010.02 > User.first._id.to_s.to_i(16).base62_encode
 => "uMarF7LmpY3bNOSn"

这篇关于如何缩短mongo ID以更好地在URL中使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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