Is there a way to push root documents to specific field on $facet without $group ? For example,
db.artwork.aggregate( [
{
$facet: {
"categorizedByTags": [
{ $unwind: "$tags" },
{ $sortByCount: "$tags" }
],
"items": [{$push: "$$ROOT"}] // I want to push all root document to `items`.
}
}])
Yes, just use an empty $match, like so:
db.artwork.aggregate( [
{
$facet: {
"categorizedByTags": [
{ $unwind: "$tags" },
{ $sortByCount: "$tags" }
],
"items": [{$match: {}}]
}
}])
You can use like this:
db.artwork.aggregate( [
{
$facet: {
items: [ { $match: {} } ],
}
}
])