- match movies by genre.name
- movies go in results
- categories will contain a set of
$genres.name
use('the_base');
function stageMatch(gen) {
return {
"$match": {
"genres.name": gen,
}
};
}
function stageFacet() {
return {
"$facet": {
"results": [],
"categories": [
{
"$unwind": "$genres"
},
{
"$group": {
"_id": 0,
"all": {
"$addToSet": "$genres.name"
}
}
}
]
}
}
}
// remove db.movie.aggregate to see the query, copy/pasta in compass the query and re-generate for any language
db.movie.aggregate([
stageMatch("War"),
stageFacet()
])
bson.A{
bson.D{
{"$match",
bson.D{
{"shop", "ewuk"},
{"category", "led-lamps"},
},
},
},
bson.D{
{"$project",
bson.D{
{"slug", 1},
{"sku", 1},
{"price", 1},
{"title", 1},
{"category", 1},
{"collection", 1},
},
},
},
bson.D{
{"$facet",
bson.D{
{"results", bson.A{}},
{"collections",
bson.A{
bson.D{{"$unwind", "$collection"}},
bson.D{
{"$group",
bson.D{
{"_id", 0},
{"all", bson.D{{"$addToSet", "$collection"}}},
},
},
},
},
},
},
},
},
}