$project exclusion followed by $project exclusion inclusion mix stops tracking metadata correctly

    • Type: Bug
    • Resolution: Unresolved
    • Priority: Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • None
    • Query Execution
    • ALL
    • Hide

      Here is a full js test blob to reproduce the issue:
       
       

      const collName = jsTestName();
      const coll = db.getCollection(collName);
      coll.drop();
       
      assert.commandWorked(coll.insertMany([
          {_id: 0, x: "hi", y: "bye"},
      ]));
       
      let onlyInclusion = [
          {$project: {x: 0}},
          {$project: {y: 1, metaField: {$meta: "textScore"}}},
      ];
      assert.commandFailedWithCode(
          coll.getDB().runCommand({aggregate: coll.getName(), pipeline: onlyInclusion, cursor: {}}),
          40218);
       
      let exclusionAndInclusion = [
          {$project: {x: 0}},
          {$project: {y: 0, metaField: {$meta: "textScore"}}},
      ];
      assert.commandFailedWithCode(
          coll.getDB().runCommand(
              {aggregate: coll.getName(), pipeline: exclusionAndInclusion, cursor: {}}),
          40218);
      

      Resmoke command:

      buildscripts/resmoke.py run --suites=aggregation jstests/<file_name>.js
      Show
      Here is a full js test blob to reproduce the issue:     const collName = jsTestName(); const coll = db.getCollection(collName); coll.drop();   assert .commandWorked(coll.insertMany([     {_id: 0, x: "hi" , y: "bye" }, ]));   let onlyInclusion = [     {$project: {x: 0}},     {$project: {y: 1, metaField: {$meta: "textScore" }}}, ]; assert .commandFailedWithCode(     coll.getDB().runCommand({aggregate: coll.getName(), pipeline: onlyInclusion, cursor: {}}),     40218);   let exclusionAndInclusion = [     {$project: {x: 0}},     {$project: {y: 0, metaField: {$meta: "textScore" }}}, ]; assert .commandFailedWithCode(     coll.getDB().runCommand(         {aggregate: coll.getName(), pipeline: exclusionAndInclusion, cursor: {}}),    40218); Resmoke command: buildscripts/resmoke.py run --suites=aggregation jstests/<file_name>.js
    • None
    • 3
    • TBD
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      If you run a query like:

       

      assert.commandWorked(coll.insertMany([
         {_id: 0, x: "hi", y: "bye"},
      ]));
       
      let onlyInclusion = [
         {$project: {x: 0}},
         {$project: {y: 1, metaField: {$meta: "textScore"}}},
      ];
      assert.commandFailedWithCode(
         coll.getDB().runCommand({aggregate: coll.getName(), pipeline: onlyInclusion, cursor: {}}),
         40218);
      

      This query will correctly fail, saying the "textScore" metadata is not available.

       

      However if you flip the second $project to exclude with regular field while including the "textScore" metadata, the query will now pass (incorrectly):

       
       

      let exclusionAndInclusion = [
         {$project: {x: 0}},
         {$project: {y: 0, metaField: {$meta: "textScore"}}},
      ];
      assert.commandFailedWithCode(
         coll.getDB().runCommand(
             {aggregate: coll.getName(), pipeline: exclusionAndInclusion, cursor: {}}),
      
         40218);

       
      I'm aware in the $project documentation that it states if you exclude fields, you should also not also include other fields , however, if you remove the first $project exclusion stage from the second query, it will start failing correctly. So, regardless, there is some inconsistency here that should be addressed.
       

      Note, its possible this is related to SERVER-107875 as these both show this difference in behavior when the query involves a $project exclusion first in the query that does not fail correctly. They may be different however, its unclear to me.

              Assignee:
              Unassigned
              Reporter:
              Joe Shalabi
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Created:
                Updated: