mongodb - mongo中如何实现自增id,删除其中一个id后,所有id重新排序?
迷茫
迷茫 2017-05-02 09:26:01
0
2
702

想用id(1-n)记录博客文章发表的次序,在网上查找到一个方法,通过增加一个sequence表,但是这种方法有个缺点,就是一旦其中某篇文章删除后,其他id不会改变,求更好的方法

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all (2)
大家讲道理

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! ! !

    漂亮男人

    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 usedObjectId完全可以满足你的需求。ObjectIditself is increasing and can be sorted.
    Regarding ObjectId, I wrote a blog to introduce related knowledge. If you are interested, please click here.

      Latest Downloads
      More>
      Web Effects
      Website Source Code
      Website Materials
      Front End Template
      About us Disclaimer Sitemap
      php.cn:Public welfare online PHP training,Help PHP learners grow quickly!