$project exclusion followed by $project inclusion of "sortKey" metadata does not fail 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"},
      ]));
       
      let query2 = [
         {$project: {metaField: {$meta: "sortKey"}}},
      ];
      assert.commandFailedWithCode(
         coll.getDB().runCommand({aggregate: coll.getName(), pipeline: query2, cursor: {}}),
         ErrorCodes.BadValue);
       
      let query1 = [
         {$project: {x: 0}},
         // {$project: {metaField: {$meta: "geoNearDistance"}}}, // query fails with 40218
         // {$project: {metaField: {$meta: "textScore"}}}, // query fails with 40218
         // {$project: {metaField: {$meta: "searchScore"}}},  // query fails with 40218
         {$project: {metaField: {$meta: "sortKey"}}},
      ];
      assert.commandFailedWithCode(
         coll.getDB().runCommand({aggregate: coll.getName(), pipeline: query1, cursor: {}}),
         ErrorCodes.BadValue);
      

       

      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" }, ]));   let query2 = [    {$project: {metaField: {$meta: "sortKey" }}}, ]; assert .commandFailedWithCode(    coll.getDB().runCommand({aggregate: coll.getName(), pipeline: query2, cursor: {}}),    ErrorCodes.BadValue);   let query1 = [    {$project: {x: 0}},     // {$project: {metaField: {$meta: "geoNearDistance" }}}, // query fails with 40218     // {$project: {metaField: {$meta: "textScore" }}}, // query fails with 40218     // {$project: {metaField: {$meta: "searchScore" }}},  // query fails with 40218    {$project: {metaField: {$meta: "sortKey" }}}, ]; assert .commandFailedWithCode(    coll.getDB().runCommand({aggregate: coll.getName(), pipeline: query1, cursor: {}}),   ErrorCodes.BadValue);   Resmoke command: buildscripts/resmoke.py run --suites=aggregation jstests/<file_name>.js
    • None
    • 3
    • TBD
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      If you try to project "sortKey" metadata when it does not exist, the query will fail correctly like:

       

      assert.commandWorked(coll.insertMany([
         {_id: 0, x: "hi"},
      ]));
       
      let query2 = [
         {$project: {metaField: {$meta: "sortKey"}}},
      ];
      assert.commandFailedWithCode(
         coll.getDB().runCommand({aggregate: coll.getName(), pipeline: query2, cursor: {}}),
         ErrorCodes.BadValue);
      

       

       

      However, if you put a $project exclusion before the same projection of the "sortKey" metadata, the query will start passing (incorrectly) like:

       

      let query1 = [
         {$project: {x: 0}},
         {$project: {metaField: {$meta: "sortKey"}}},
      ];
      assert.commandFailedWithCode(
         coll.getDB().runCommand({aggregate: coll.getName(), pipeline: query1, cursor: {}}),
         ErrorCodes.BadValue);
      

      This seems specific to "sortKey" metadata, as other metadata types like "geoNearDistance", "textScore", and "searchScore" fail correctly in the second case (I didn't try every metadata possibility).

       

      Note, its possible this is related to SERVER-107874 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:
              3 Start watching this issue

                Created:
                Updated: