-
Type: Bug
-
Resolution: Fixed
-
Priority: Major - P3
-
Affects Version/s: 7.0.0, 6.0.6
-
Component/s: None
-
Query Optimization
-
Fully Compatible
-
ALL
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.