[SERVER-29930] allow nested $facet in another $facet of aggregate Created: 30/Jun/17  Updated: 23/Feb/23

Status: Backlog
Project: Core Server
Component/s: Aggregation Framework
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Major - P3
Reporter: Jame Yang Assignee: Backlog - Query Optimization
Resolution: Unresolved Votes: 7
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to SERVER-66707 Add ability to nest $group operations Backlog
Assigned Teams:
Query Optimization
Participants:

 Description   

Like below code:

db.test.aggregate([
  {
    "$facet": {
      "allGroups": [
        {
          "$facet": {
            "group1": [{
              "$group":{"_id": "$item1"}
            }],
            "group2": [{
              "$group":{"_id": "$item2"}
            }]
          }
        }
      ],
      "size": [
        {
          "$count": "size"
        }
      ]
    }
  }
])

This will thrown:

"errmsg" : "specified stage is not allowed to be used within a $facet stage: 0: { $facet: { group1: [ { $group:

Unknown macro: { _id}

} ], group2: [ { $group:

Unknown macro: { _id}

} ] } }",
"code" : 40331,
"codeName" : "Location40331"

Hope to allow nested $facet happen, is there any downside?



 Comments   
Comment by Asya Kamsky [ 23/Feb/23 ]

The example query actually doesn’t require nested $facet - you can post your example/query in community.mongodb.com forums though.

Comment by Shail Panchal [ 23/Feb/23 ]

Is there any workaround I can implement if I want to solve a similar kind of query? 

Generated at Thu Feb 08 04:22:11 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.