I am making a web service that serves the data in JSON format. It does so by taking some filters in the form of an array, these filters are then parsed and then checked if any of the values from filter exists in a document's specific field (The field is also an array), it returns all such documents.
Here, filter is the query array. typeFilters is the array in every document. I am using $project to implement it.
This is my code:
function returnAllByFilter(filter, callback) {
ModuleDBService.aggregate([{
$project: {
_id:1,
title:1,
typeFilters:1,
filteredResults: {
$let: {
vars: {
filter: [0,2],
},
in: {
$setIntersection: ["$$filter", "$typeFilters"]
}
}
}
}
},
{
$match: {
filteredResults: {
$ne: []
}
}
}
], function (err, data) {
if (err) {
console.log(err)
callback(null);
} else {
callback(data);
}
})
}
This code works fine, but it also projects the field filteredResults.
Sample of output:
[
{
"_id": "3133343230313834754e5871",
"title":"Physics 202:1",
"typeFilters": [
0,
1,
3
],
"filteredResults": [
1
]
}
]
I tried to remove the field from the query and replaced it with the contents inside it directly, "I got errors saying "unknown top level operator: $let". How can I prevent this query from projecting the filteredResults column as well?
Also, is there a better practice to implement this functionality?