I've now tested this via the shell, Studio 3T IDE and within my API itself.
The first query looks something like this:
Notification.find({
userId: ObjectId("...")
}).limit(20).sort('-createdAt')
which returns the following documents sorted by their createdAt
timestamp (which is also imbedded in the _id
field):
I then run the next query which I would expect to return the results starting at _id: ObjectId("615869eac849ec00205aa112")
:
Notification.find({
userId: ObjectId("..."),
_id: { $lt: ObjectId("615869eac849ec00205aa115"}
}).limit(20).sort('-createdAt')
I would expect this command to get me my next 20 results sorted in the same descending order as the original query above. However, I get the following:
which has 3 results from the original query. The _id
field is clearly unique between the _id
I use as a cursor and the incorrectly returned results but after inspection the createdAt
timestamp is the exact same as the createdAt
timestamp of the document _id
I use for the range query.
CodePudding user response:
The problem is you are querying on an unsorted field expecting that value to identify a specific point the result set.
Note that in the first result set entries 17 through 29 all have the same epoch timestamp in the _id value, and that those 13 entries are not in any particular order.
As luck would have it, entry 20 has the greatest _id of that group, so all 12 of the others are lesser, even the ones that happened to come before.
To make this work, also sort on _id like:
.sort({createdAt: -1, _id: -1})