You may use something like this to get all tags per document:
db.collection.aggregate([
{
$project: {
_id: 0,
tags: {
$reduce: {
input: "$posts.post.tags",
initialValue: [],
in: {
"$setUnion": [
"$$value",
"$$this"
]
}
}
}
}
}
])
Explained:
Project the array posts.post.tag and apply reduce operation with $setUnion between all documents tag array elements to receive distinct list of tags
playground
And something like this to take distinct tags per collection:
db.collection.aggregate([
{
$project: {
_id: 0,
tags: {
$reduce: {
input: "$posts.post.tags",
initialValue: [],
in: {
"$setUnion": [
"$$value",
"$$this"
]
}
}
}
}
},
{
$group: {
_id: "Total",
tags: {
"$push": "$tags"
}
}
},
{
$project: {
_id: 0,
tags: {
$reduce: {
input: "$tags",
initialValue: [],
in: {
"$setUnion": [
"$$value",
"$$this"
]
}
}
}
}
}
])
Explained:
1.project all tags per document.
2. Group all tags from collection.
3. setUnion to all tags arrays in collection.
playground2