A
A
Andrey2020-09-22 20:36:37
JavaScript
Andrey, 2020-09-22 20:36:37

How to select from mongodb database after sorting?

Hello. tell me how to do this in mongodb?
There is a base to which is added every minute of the record.
There is a page that a person comes to and, by endless scrolling, can view the history of additions

. records are added constantly this did not work

.find({})
        .sort({created_at: -1})
        .skip(page > 0 ? page * onPage : 0)
        .limit(onPage)

Now the idea is to remember the id of the document that was first in sorting by date. and thief start first until the next page reload, logically, I think so is there something similar in mongo? or where to dig:
.find({})
        .sort({created_at: -1})
        .start({_id: startId})
        .limit(onPage)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Robur, 2020-09-23
@Robur

but because records are added constantly this did not work

what exactly "didn't work"?
if you sort by creation date, then new entries should always be added to the end of the list, so it should work fine - if you look from 50 to 100 and the 1001st was added there, then the order of the first 1000 should not change, and to load the next from 100 150 each, this addition should not affect

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question