I want to use ID (1-n) to record the order in which blog articles are published. I found a method on the Internet by adding a sequence table. However, this method has a disadvantage. Once an article is deleted, other IDs will be deleted. It won’t change, please find a better way
大家讲道理2017-05-02 09:28:01
Your idea is probably difficult to implement in MongoDB. Your idea comes from the identity counter provided in many relational databases.
In MongoDB, if you need to sort blog posts, you can use time or other "fields" suitable for sorting.
For reference.
Love Mongo! Have Fun!
-->Poke me<--Please poke me on the left, it’s April! Sign up now!
Registration for the MongoDB Chinese Community Shenzhen User Conference has begun! The master of this forum will make a grand appearance at the conference and deliver a speech. There is applause here! ! !
漂亮男人2017-05-02 09:28:01
If I understand correctly, no database can fulfill your request. Suppose I have 10,000 blogs now and I delete the first one. Do you want to regenerate IDs for the next 9,999 posts? What if there are 1 million blogs in total? So this idea itself is incorrect.
If the purpose is only to record the order in which documents are published, the default _id used ObjectId
完全可以满足你的需求。ObjectId
itself is increasing and can be sorted.
Regarding ObjectId, I wrote a blog to introduce related knowledge. If you are interested, please click here.