[SERVER-80456] Wrong error message when using $facet inside $facet Created: 27/Aug/23  Updated: 25/Jan/24

Status: In Progress
Project: Core Server
Component/s: None
Affects Version/s: 7.0.0, 6.0.6
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Andre Spiegel Assignee: Milena Ivanova
Resolution: Unresolved Votes: 0
Labels: neweng, quick-tech-debt
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Assigned Teams:
Query Optimization
Operating System: ALL
Participants:

 Description   

When I try to run the aggregation below, which has a nesting of $facet -> $lookup -> $facet -> $lookup, I get the error message "$lookup is not allowed to be used within a $facet stage". This message is wrong, since $lookup is indeed allowed in a $facet stage and works fine. The problem with the pipeline is that it has a $facet inside a $facet, which is not allowed.

This is the pipeline that results in the wrong error message:

db.data.aggregate([
    { "$facet" : {
        "result" : [
            { "$match" : { "ref1" : { "$exists" : true }}},
            { "$lookup" : {
                "from" : "data",
                "localField" : "ref1",
                "foreignField" : "_id",
                "pipeline" : [
                    { "$facet" : {
                        "result" : [
                            { "$match" : { "ref2" : { "$exists" : true }}},
                            { "$lookup" : {
                                "from" : "data",
                                "localField" : "ref2",
                                "foreignField" : "_id",
                                "as" : "ref2" }}]}}],
                "as" : "ref1" }}]}}]) 

On the other hand, for a pipeline with a $facet immediately nested within another $facet, the message is correct. This pipeline:

db.data.aggregate([{"$facet" : { "x" : [ {"$facet" : { "y" : [] }}]}}]) 

results in the correct message "$facet is not allowed to be used within a $facet stage"

The problem is the same in both 6.0.6 and 7.0.0.


Generated at Thu Feb 08 06:43:36 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.