I have a collection like the following, and the target is to delete each document which matches to it's items.fruits.color
AND items.fruits.type
The problem is, I need to find the fruit which has both conditions together.
Also, if the items
key is empty, i need to select that document
This is the state of my aggregation:
{
type: "x"
color: "red"
items => [
{
item_id: 6
fruits: [
{
color: "red",
type: "x"
},
{
color: "red",
type: "y"
}
]
},
{
item_id: 7
fruits: [
{
color: "orange",
ref_id: "h"
},
{
color: "green",
ref_id: "x"
}
]
}
]
},{
type: "o"
color: "red"
items => []
},{
type: "w"
color: "red"
items => [
{
fruits: [
{
type: "w",
color: "black"
}
]
}
]
}
So the first document is invalid, because attributes of items[fruits][0]
is exactly match.
But the second one is valid, because the items array is empty.
The third one is also valid because there is no matching fruit's inside it's items.
The last query i tried was:
$match: {
"items.fruits": {
$not: {
$elemMatch: {
color: "$color",
type: "$type"
}
}
}
}
But it's not working. instead it's showing all documents. (I think it's because the $color statement is not recognized as a variable)