I'm trying to use the lean()
option in order to speed up my queries. But when adding it to a query like this:
Pets.aggregate({ $match: { 'Name': 'Floofkins', 'lastMealDate': {$gt: today}}}, function (err, pets){
if (err) {
res.status(500).json({'error': err});
console.log('Could not fetch pets: ' + err);
return;
}
petsHadMealsToday = pets.length;
}).lean();
All I get is TypeError: Cannot read property 'lean' of undefined
although pets.length
returns the number of pets that matched the query.
If I'd remove the match
option however and run something like below, it works like a charm.
Pets.aggregate({ 'Name': 'Floofkins', 'lastMealDate': {$gt: today}}, function (err, pets){
if (err) {
res.status(500).json({'error': err});
console.log('Could not fetch pets: ' + err);
return;
}
petsHadMealsToday = pets.length;
}).lean();
I guess I'm missing some fundamental point about how to use match
and such, so feel free to educate me!