I want to get all items (or better ids of items) that have missing tags.
If a child item misses the tag of its parent it should be outputted.
The documents looks like this:
[
{
id: 1,
tags: [
"a",
"b"
],
childs: [
2,
3
]
},
{
id: 2,
tags: [
"a"
],
childs: []
},
{
id: 3,
tags: [],
childs: []
},
{
id: 4,
tags: [
"c"
],
childs: [
5
]
},
{
id: 5,
tags: [
"c"
],
childs: []
},
{
id: 6,
tags: [
"b"
],
childs: [
5
]
},
{
id: 7,
tags: [],
childs: []
},
]
Now I want to search by a tag name to get items with missing tag.
The desired result should look like this:
Check for tag "a":
{
id: 3,
...
}
or
{
ids: [3]
}
Check for tag "b":
{
id: 2,
...
},
{
id: 3,
...
}
or
{
ids: [2, 3]
}
I tried aggregation with the lookup and pipeline function, but did not got it working.
[{
$match: {
tags: "a",
childs: {
$ne: []
}
}
}, {
$lookup: {
from: 'collection1',
localField: 'childs',
foreignField: 'id',
as: 'childs_items',
pipeline: [{
$matching : {
"tags": {
$nin: "a"
}
}
}]
}
}]
What would be the best approach?
EDIT: changed document example tags of last two docs to "c" EDIT2: added example data