Answer the question
In order to leave comments, you need to log in
How to find the latest documents in a collection with a where condition?
There is a collection of several million documents, each entry has a date: timestamp, you need to display the latest entries, where date: timestamp > current timestamp - 5, i.e. records for the last 5 seconds. They are expected in the response somewhere from 0 to 300.
I tried to use the $where function, but the response takes a very long time, as far as I understand, this is a resource-intensive operation.
Please tell me the best way to make this request.
Answer the question
In order to leave comments, you need to log in
It is desirable to add an index to this field.
collection.find({
'date': {
$gte: new Date(Date.now() - 5000) // 5000ms = 5 секунд
}
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question