How does MongoDB get neighboring records of a record

  mongodb, question

For example, to obtain the previous and the following record of an article, there is a time field.created

Try to get the article using the following statement

# created 是当前文章的创建时间
# 前一篇
prev_post = db.Post.find({ 'created': { '$lt': created } }, sort = [('created', -1)], limit = 1)
# 后一篇
next_post = db.Post.find({ 'created': { '$gt': created } }, sort = [('created', 1)], limit = 1)

The result of the operation is that these records are made by jumping, sometimes several records will be skipped in the middle, and I don’t know how to solve this problem.
I think it may be that my understanding of find is wrong. I hope you can give me some advice.

First of all, there is no problem with the statement, but you have used the time type.
If the time type is the same, you will probably encounter the same time record. Please check if your record has the same time.
If conditions permit, you can use_idOr cooperate_idTo avoid this situation.