Here I've just created a collection of 9 documents {id, name}. All the documents have the same value "A" of the field name.
[
{
"name": "A",
"id": 1
},
{
"name": "A",
"id": 2
},
{
"name": "A",
"id": 3
},
{
"name": "A",
"id": 4
},
{
"name": "A",
"id": 5
},
{
"name": "A",
"id": 6
},
{
"name": "A",
"id": 7
},
{
"name": "A",
"id": 8
},
{
"name": "A",
"id": 9
}
]
I'm want to paginate this collection after sorting by name (it is useless to sort by name in my case, but I do that to prouve the strange behaviour), 3 by 3. (Page size is 3).
When I execute an aggregate pipeline with $skip 0, $limit 3 (first page):
db.collection.aggregate([
{
"$sort": {
"name": 1
}
},
{
"$skip": 0
},
{
"$limit": 3
}
])
The result is :
[
{
"_id": ObjectId("5a934e000102030405000000"),
"id": 1,
"name": "A"
},
{
"_id": ObjectId("5a934e000102030405000001"),
"id": 2,
"name": "A"
},
{
"_id": ObjectId("5a934e000102030405000002"),
"id": 3,
"name": "A"
}
]
And now when I want to get the following page ($skip 3, $limit 3), the result is:
[
{
"_id": ObjectId("5a934e000102030405000000"),
"id": 1,
"name": "A"
},
{
"_id": ObjectId("5a934e000102030405000002"),
"id": 3,
"name": "A"
},
{
"_id": ObjectId("5a934e000102030405000005"),
"id": 6,
"name": "A"
}
]
We remark that documents with ids 1 and 3 are extracted again. This ends up with a bad pagination (duplicates)!
How do you explain this, when sorting on non unique column leads to this strange behaviour?
To reproduce the problem https://mongoplayground.net/p/hP7CMtA3b2f