Hello everyone!
I’m trying to do a paginated, filtered and sorted search using MongoDB Realm Functions. For the pagination I’m using skip() and limit(), while for the filtering and sorting I’ve tried both the regular .find({query}) method and the aggregate() method. They both return results but not the expected ones. In both cases, if a filter is provided, the sorting only applies for the 12 documents fetched for that particular pagination call, resulting in improper sorting. In both cases, if I take out the query in the find() method or in the $match object, the results are sorted properly but the filtering is essential to the feature I’m building. Maybe I’m doing something wrong, I’m not sure but I’ll leave code samples for both, maybe someone can lend a hand.
Find() method:
var result = await collection.find(composedFilter).sort({price: 1}).skip(12).limit(12);
Aggregate method:
var result = await collection.aggregate([{$match: composedFilter}, {$sort:{price: 1}}, {$skip: 12}, {$limit: 12}]);
If you need more context, please feel free to let me know and I’ll provide. Thanks a lot!!