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

Query shape for $documents is unique on each execution

    • Query Integration
    • Minor Change
    • ALL
    • v7.0

      When running an aggregation with a $documents stage, the query shape comes from a rewritten query that is unique each time the query runs. The pipeline in the query shape looks like this, with different UUIDs each time the query runs:

              {
                      "$queue" : "?array<?object>"
              },
              {
                      "$project" : {
                              "_id" : true,
                              "03e7f159-305d-4009-9eb8-37f849cb26cb" : "?array<?object>"
                      }
              },
              {
                      "$unwind" : {
                              "path" : "$03e7f159-305d-4009-9eb8-37f849cb26cb"
                      }
              },
              {
                      "$replaceRoot" : {
                              "newRoot" : "$03e7f159-305d-4009-9eb8-37f849cb26cb"
                      }
              }
      

      The script below runs such a query twice (run with ./mongo --nodb example.js or using resmoke.py). After running it, search in the output for Query Stats: and notice that there are two entries with distinct query shapes.

      const options = {setParameter: {internalQueryStatsRateLimit: -1}};
      
      const conn = MongoRunner.runMongod(options);
      const testDB = conn.getDB("test");
      const coll = testDB.coll;
      assert.commandWorked(coll.insert([{v: 1, y: -3}, {v: 2, y: -2}]));
      
      {
          const pipeline = [
              {$documents: [{v:1}, {v:2}]},
              {$lookup: {
                  from: coll.getName(),
                  as: "lookedUp",
                  localField: "v",
                  foreignField: "v"}
              },
          ];
      
          const cmd = {
              aggregate: 1,
              pipeline: pipeline,
              cursor: {},
          };
      
          // Run twice to see if the query shape differs each time
          assert.commandWorked(testDB.runCommand(cmd));
          assert.commandWorked(testDB.runCommand(cmd));
      
          const result = conn.adminCommand({
              aggregate: 1,
              pipeline: [
                  {$queryStats: {}},
                  {$sort: {key: 1}},
                  {$match: {"key.client.application.name": "MongoDB Shell"}}
              ],
              cursor: {}
          });
          assert.commandWorked(result);
          print("Query Stats: " + tojson(result.cursor.firstBatch));
      }
      
      MongoRunner.stopMongod(conn);
      

            Assignee:
            charlie.swanson@mongodb.com Charlie Swanson
            Reporter:
            ryan.berryhill@mongodb.com Ryan Berryhill
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: