[SERVER-50504] $facet+$match on positional path doesn't seem to pass results to certain subsequent stages Created: 24/Aug/20  Updated: 29/Oct/23  Resolved: 15/Oct/20

Status: Closed
Project: Core Server
Component/s: Aggregation Framework
Affects Version/s: 4.2.1, 4.4.0
Fix Version/s: 4.9.0

Type: Bug Priority: Critical - P2
Reporter: Valeri Karpov Assignee: Ruoxin Xu
Resolution: Fixed Votes: 0
Labels: qexec-team
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to SERVER-1831 Using dot-notation to field-select an... Backlog
Backwards Compatibility: Fully Compatible
Operating System: ALL
Steps To Reproduce:

See description

Sprint: Query 2020-09-21, Query 2020-10-05, Query 2020-10-19
Participants:

 Description   

$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
      }
    ]
  }
]



 Comments   
Comment by Githook User [ 15/Oct/20 ]

Author:

{'name': 'Ruoxin Xu', 'email': 'ruoxin.xu@mongodb.com', 'username': 'RuoxinXu'}

Message: SERVER-50504 $facet+$match on positional path does not seem to pass results to certain subsequent stages
Branch: master
https://github.com/mongodb/mongo/commit/b4d1ffd5c9474f6b3665b2fb0f886005c6cd91cc

Comment by Valeri Karpov [ 27/Aug/20 ]

Thanks! I'll keep an eye out for updates.

Comment by Eric Sedor [ 27/Aug/20 ]

Thanks val@karpov.io, I can see the behavior you're reporting. Besides $count, it looks like $project and $group are also impacted:

replset:PRIMARY> db.test.aggregate([   {
     $facet: {
       scoreRank: [
         { $match: { 'quizzes.0.score': { $gt: 100 } } },{$count:"count"}       ]     }   } ]).toArray()
[ { "scoreRank" : [ ] } ]
replset:PRIMARY>  db.test.aggregate([   {
     $facet: {
       scoreRank: [
         { $match: { 'quizzes.0.score': { $gt: 100 } } },{$project:{_id:1}}       ]     }   } ]).toArray()
[ { "scoreRank" : [ ] } ]
replset:PRIMARY> db.test.aggregate([   {
     $facet: { 
       scoreRank: [
         { $match: { 'quizzes.0.score': { $gt: 100 } } },{ $group: { _id: null, myCount: { $sum: 1 } } }       ]     }   } ]).toArray();
[ { "scoreRank" : [ ] } ]

I linked up SERVER-1831 which seems related but is not necessarily a cause, and am passing this on for consideration. You can watch this ticket for updates as they become available.

Sincerely,
Eric

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