The problem involves querying MongoDB for a collection of documents within a specified time range and retrieving a subset of 100 documents from that collection.
I have a collection in mongoDB and I need to access 100 documents within a time period. For example, if I said begin: 15/03/2023 end: 17/03/2023, it needs to know how many values are within this range and get 100 of them.
The way I'm doing it now is requesting mongoDB the number of documents within this range and divide for 100.
var period = {
timestamp: {
'$gte': new Date(dateBegin),
'$lt': new Date(dateEnd)
}
};
totalDocuments = await db.collection(document).find(period, { projection: projection }).count({});
jumps = Math.round(totalDocumentsRes / 100);
Then I do a for loop and inside of this for i request the values in the positions
var idx;
for(var i = 1; i <= 100; i++){
idx = i * jumps;
values.push((await db.collection(documents).find(period, { projection: projection }).skip(idx).limit(1).sort({ timestamp: -1 }).toArray())[0]);
}
With this metod I takes 20s to get only 5 documents. Is there anyway I can improve this algorithm?