I need to switch a Collection.find(demandObject) query to a Collection.aggregate() function and i can't get things together correctly.
My Collection:
"movies": [
{
"_id": "5e34800b13ad37006c550182",
"name": "Matrix",
"genres": [
"5e2d8a3e503465004beeeb4d",
"5e2d8a3e503465004beeeb4e",
"5e2d8a3e503465004beeeb55"
],
"themes": [
"5e2ddd90783eeb0102da66cd"
]
},
{
"_id": "5e34800b13ad37006c550182",
"name": "Matrix 2",
"genres": [
"5e2d8a3e503465004beeeb4f",
"5e2d8a3e503465004beeeb44",
"5e2d8a3e503465004beeeb56"
],
"themes": [
"5e2ddd90783eeb0102da66c7"
]
},
]
I have the following query:
Collection.find({
"genres": {
"$all": [
"5e2d8a3e503465004beeeb4e",
"5e2d8a3e503465004beeeb4a"
]
},
"themes": {
"$all": "5e2ddd90783eeb0102da66be"
},
"name": {
"$regex": "Matrix",
"$options": "i"
}
});
My approach
Collection.aggregate([
{ "$match":
{
"$and": [
{"name": { "$regex": "Matrix", "$options": "i" }},
{"themes": { "$all": ["5e2ddd90783eeb0102da66be"] }},
{"genres": { "$all": ["5e2d8a3e503465004beeeb4e", "5e2d8a3e503465004beeeb4a"] }}
]
}
}
]).exec();
The find method returns the collection how i want but the aggregation method response is always empty. What am i doing wrong?