UPDATE:
I managed to get this working.
I built up the cond
using a map function to iterate through the req.query params.
I’ve managed to get one step further.
So I’m creating an object out of the query params. And I am passing the values to the $filter.
Is there anyway to pass an empty value if that param is not in the url and still get a successful result back?
My schema looks like:
const projectSchema = new mongoose.Schema({
project_title: {
'type': String
},
issues: [
{
"issue_title": {
'type': String
},
"issue_text": {
'type': String
},
"created_by": {
'type': String
},
"assigned_to": {
'type': String
},
"status_text": {
'type': String
},
"created_on": {
'type': Date
},
"updated_on": {
'type': Date
},
"open": {
"type": Boolean
}
}
]
});
I am trying to find a doc by project_title
and then filter the issues
array by query parameters.
console.log(params); // { assigned_to: 'pluto' }
Project.aggregate(
[
{
$match: {
'project_title': 'project test'
}
}, {
$project: {
issue: {
$filter: {
input: '$issues',
as: 'item',
cond: { $and: [
{$eq: ['$$item.assigned_to', queryParams.assigned_to ]},
{$eq: ['$$item.created_by', queryParams.created_by ]}
]}
}
}
}
}
]
).exec((err, issues) => {
if (err) throw err;
console.log('issues ', issues);
return res.json(issues);
})