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

$facet+$match on positional path doesn't seem to pass results to certain subsequent stages

    • Fully Compatible
    • ALL
    • Hide

      See description

      Show
      See description
    • Query 2020-09-21, Query 2020-10-05, Query 2020-10-19

      $matching on a positional path only seems to work as expected within a $facet if there are no subsequent stages. $count, $project, $group all behave as though they receive zero input documents, even though results are correctly returned when no subsequent stage is provided.

      $sort, however, seems to function correctly.

      original description

      The below script leads to an empty scoreRank:

      db.test.drop();
      
      db.test.insertMany([{
         "_id": 12345,
         "quizzes": [
           { "score": 100 }
         ]
        },
        {
          "_id": 789,
          "quizzes": [
            { "score": 200 }
          ]
        }
      ]);
      
      const res = db.test.aggregate([
        {
          $facet: {
            scoreRank: [
              { $match: { 'quizzes.0.score': { $gt: 0 } } },
              { $count: 'count' }
            ]
          }
        }
      ]).toArray();
      
      print(JSON.stringify(res, null, '  '));
      

      I get the below output:

      [
        {
          "scoreRank": []
        }
      ]
      

      If you replace 'quizzes.0.score' with 'quizzes.score', move $match out of $facet, or move the entire scoreRank pipeline out of $facet I get the expected result:

      [
        {
          "scoreRank": [
            {
              "count": 2
            }
          ]
        }
      ]
      

            Assignee:
            ruoxin.xu@mongodb.com Ruoxin Xu
            Reporter:
            val@karpov.io Valeri Karpov
            Votes:
            0 Vote for this issue
            Watchers:
            11 Start watching this issue

              Created:
              Updated:
              Resolved: