everyone! I'm looking for a solution to my problems around the internet a few days and after a lot of failed tries, I decided to ask my first question here.
So, I have a model like this:
const Exercise = new Schema (
{
description: String,
duration: Number,
date: Date
},
{
_id: false
}
);
const User = new Schema({
username: String,
log: [
Exercise
]
});
And I would like to find an specific user and filter his/her exercise log in a date range. If there isn't any exercise in the range, I'd just like to return the id, username and the log as an empty array. So, I've built this query:
User.aggregate([
{
$match: {
_id: userIdInput
}
},
{
$project: {
_id: 1,
username: 1,
log: {
$filter: {
input: "$log",
as: "log",
cond: {
$gte: [ "$$log.date", from ]
}
}
}
}
}])
.exec()
.then((log) => {
res.json(log);
})
.catch(err => {
next(err);
});
}
But this query always return an empty array. Is there anything wrong I'm doing or any tips to compose this query in a better way?
I really appreciate any help.