Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
176
rated 0 times [  180] [ 4]  / answers: 1 / hits: 6690  / 10 Years ago, wed, january 14, 2015, 12:00:00

I need to use skip and limit for pagination, and the distinct for not return equal values.



If i use



MyModel.find().distinct('blaster', function(err, results) {
res.render('index', {
data: results
});
});


This works.



If i use



MyModel.find().sort('brand').skip((page-1)*15).limit(15).exec(function(err, results) {
res.render('index', {
data: results
});
});


This is also working, but how use both?



If i try, the error will show:



Error: skip cannot be used with distinct

More From » node.js

 Answers
19


You don't do that. .distinct() is a method that returns an array, and therefore you cannot modify something that is not a Cursor with cursor modifiers like .limit() and .skip().



What you want is the .aggregate() method. Much more than just adding things up:



MyModel.aggregate(
[
{ $group: { _id: $blaster } },
{ $skip: ( page-1 ) * 15 },
{ $limit: 15 }
],
function(err,results) {
// results skipped and limited in here
}
);


The aggregation framework provides another way to achieve distinct results. But in a more flexible way. See the operators for $group, $skip and $limit.


[#40006] Tuesday, January 13, 2015, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
madalyngriseldas

Total Points: 167
Total Questions: 92
Total Answers: 85

Location: Iceland
Member since Sat, Sep 17, 2022
2 Years ago
;