UPDATE: I’ve decided to do this a different way,
I’m returning the data first and then manipulating the array.
const getLogs = function (userid, from, to, limit) {
try {
if (userid && from) {
const exerciseLogs = UserModel.findOne(
{
"_id": userid,
log: { // log is the name of the sub-array
$elemMatch: {
date: {
$gte: new Date(from)
}
}
}
}
);
return exerciseLogs; // returns same result as: else if (userid)
} else if (userid) {
exerciseLogs = UserModel.findOne({ "_id": userid });
return exerciseLogs;
}
return exerciseLogs;
} catch (e) {
console.log("e: " + e);
}
};
The else if
works perfectly from this url: /api/exercise/log?userId=f72MONg0H
But the first if statement (if (userid && from) )
returns the same result from this url:
/api/exercise/log?userId=f72MONg0H&from=2018-12-07
I can’t figure out if it’s ignoring $elemMatch