Flask+MongoDB做的一個(gè)網(wǎng)站,文章管理塊,Mongodb中以O(shè)bjectId作的標(biāo)識(shí),當(dāng)瀏覽指定文章時(shí)現(xiàn)在以O(shè)bjectId作為標(biāo)識(shí),url形式如下:
/article/53219ebcbe238721a805d3fe/
ObjectId這截有沒有好辦法處理的更短,更優(yōu)雅些?
Check this out: http://docs.mongodb.org/manual/tutorial/create-an-auto-incrementing-field/
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 :-)
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.
Create a new field to store the ID. When storing, take a screenshot.
Or use a customized Slug.
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存儲(chǔ)的數(shù)據(jù),標(biāo)識(shí)怎樣在URL顯示的更簡(jiǎn)潔
is better and easier for people to understand.