Let's say my MongoDB records look like this:
- {category: A , sub_category : 1}
- {category: A , sub_category : 2}
- {category: A , sub_category : 3}
- {category: B , sub_category : 1}
- {category: B , sub_category : 2}
- {category: B , sub_category : 3}
- {category: C , sub_category : 1}
- {category: C , sub_category : 2}
- {category: C , sub_category : 3}
I am trying to achieve pagination with these values. So, I created a compound index with both of these fields (both in ascending order).
Let's say my page limit is 2.
So my first query looks like this -> db.record.find().sort({category: 1, sub_category: 1}); -> And the first two records are returned.
My second query looks like this -> db.record.find({category : {$gte: A}, sub_category: {$gt : 2}}).sort({category: 1, sub_category: 1}).
I was expecting record 3 and 4 to return. But I got record 3 and record 6.
I understood that my way of querying is not right. I couldn't find another solution. Please help me with this issue.
P.S. I am not using skip and limit technique, because I am following bucket pattern.