alexmoon
alexmoon

Reputation: 466

mongodb facet with root

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`.
    }
  }])

Upvotes: 1

Views: 1452

Answers (2)

Tom Slabbaert
Tom Slabbaert

Reputation: 22276

Yes, just use an empty $match, like so:

db.artwork.aggregate( [
  {
    $facet: {
      "categorizedByTags": [
        { $unwind: "$tags" },
        { $sortByCount: "$tags" }
      ],
      "items": [{$match: {}}]
    }
  }])

Upvotes: 2

shubham yadav
shubham yadav

Reputation: 185

You can use like this:

db.artwork.aggregate( [
{
 $facet: {
   items: [ { $match: {} } ],
  }
 }
])

Upvotes: 1

Related Questions