Flask+MongoDB做的一个网站,文章管理块,Mongodb中以ObjectId作的标识,当浏览指定文章时现在以ObjectId作为标识,url形式如下:
/article/53219ebcbe238721a805d3fe/
ObjectId这截有没有好办法处理的更短,更优雅些?
阿神2017-04-22 09:01:58
Check this out: http://docs.mongodb.org/manual/tutorial/create-an-auto-incrementing-field/
伊谢尔伦2017-04-22 09:01:58
ObjectId has 12 bytes and a total of 96 bits of information. If hexadecimal is used, each bit can provide 6 bits of information, so only 16 64-bits are needed. There are 62 letters + numbers in total, just find two punctuation marks :-)
怪我咯2017-04-22 09:01:58
You can use numbers to increase your ID
Mongodb officially provides a solution for self-increasing ID, please refer to
http://docs.mongodb.org/manual/tutorial/create-an-auto-incrementing-field/
The main principle is to create a new collection of counter counts in the database, record the maximum ID of each model, and then update the ID when saving. Use $inc in findAndModify to achieve increment. $inc is an atomic operation.
阿神2017-04-22 09:01:58
Create a new field to store the ID. When storing, take a screenshot.
Or use a customized Slug.
大家讲道理2017-04-22 09:01:58
If it is an interface type, it will not be displayed directly in the browser address bar, so just use ObjectId.
If you are displaying an article, use: http://web/article/MongoDB存储的数据,标识怎样在URL显示的更简洁
is better and easier for people to understand.