I have a list of images that I get with Angular using $http.get() from MongoDB using Mongoose and Expressjs.
What I did works fine but I have a doubt about performance.
So far I found two ways to do it:
- using
skip - using
$nin
The queries looks like this:
// Using $nin:
var skip = req.query.skip || [];
User.find({ _id : { $nin: skip }})
.sort({ _id: -1 })
.limit(15)
.exec(function(err, users) {
if (err) res.json({ 'msg': 'Error loading users' });
res.json({
users: users
});
});
and:
// Using skip
User.find({})
.sort({ _id: -1 })
.skip(15)
.limit(15)
.exec(function(err, users) {
if (err) res.json({ 'msg': 'Error loading users' });
res.json({
users: users
});
});
Googleling around it seems like using skip lack of performance after a while...
But looking at the $nin option I found after scrolling and scrolling a very long query... with plenty of _id...
Which of the 2 solution should be better to use?
Or there is a third way much better than those, maybe I'm doing something wrong?
Thanks
lean()option to reduce time that it takes mongoose to build the model with all of it's methods etc. IMO theskipoption is better here (that's how I implement my paging solutions and never ran into any problems)lean()and write a working example please? The documentation forlean()lack of every kind of information. Thanks.leanoption tells mongoose to not include it's methods to the results, so you won't be able to use mongoose on the results you will get. You can use it like this:User.find({}).sort({ _id: -1 }).skip(15).limit(15).lean().exec(...). It will reduce the time it takes to get the results from db and will act almost as fast as the native mongo client.lean()has nothing to do with this. Please do not make misleading comments..skip(15).limit(15)seems that it doesn't work properly... I see always duplications.