Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-80456

Wrong error message when using $facet inside $facet

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 8.1.0-rc0
    • 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.

            Assignee:
            milena.ivanova@mongodb.com Milena Ivanova
            Reporter:
            andre.spiegel@mongodb.com Andre Spiegel
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: