Search code examples
mongodbprofilingprofileranalysisnested

How to search MongoDB-Documents on different nesting levels?


I'm currently analyzing a system.profile-Collection of a MongoDB-database. I'd like to find Queries that have a stage with an COLLSCAN oder IXSCAN. My problem is, that the field stage can occur on several levels (...: shortened JSON):

{
    "op" : "query",
    "ns" : "spt.coll",
    "query" : {
        "user" : "userC"
    },
    "ntoreturn" : 1,
    ...
    "millis" : 0,
    "execStats" : {
        "stage" : "PROJECTION",
        "nReturned" : 1,
        ...
        "transformBy" : {
            "settings.arr" : 1
        },
        "inputStage" : {
            "stage" : "FETCH",
            "nReturned" : 1,
            ...
            "alreadyHasObj" : 0,
            "inputStage" : {
                "stage" : "IXSCAN",
                "nReturned" : 1,
                ...
                "matchTested" : 0
            }
        }
    },
    "ts" : ISODate("2015-07-30T09:16:22.551Z"),
    "client" : "127.0.0.1",
    "allUsers" : [ ],
    "user" : ""
}

In the above example the stage occurs on 3 levels:

  1. "execStats.stage"
  2. "execStats.inputStage.stage"
  3. "execStats.inputStage.inputStage.stage"

There might be even deeper nested stages. Is there a way to return all documents, that have a stage: "IXSCAN" oder stage: "COLLSCAN" on any of those nesting levels? or do I have to run a query for each nesting level?

I tried to use following function referring to How to find MongoDB field name at arbitrary depth , but unfortunately that gives an error:

db.system.profile.find(
  function () {
    var findKey = "stage",
        findVal = "COLLSCAN";

    function inspectObj(doc) {
      return Object.keys(doc).some(function(key) {
        if ( typeof(doc[key]) == "object" ) {
          return inspectObj(doc[key]);
        } else {
          return ( key == findKey && doc[key] == findVal );
        }
      });
    }
    return inspectObj(this);
  }
)

Error-message:

Error: error: {
    "$err" : "TypeError: Object.keys called on non-object\n    at Function.keys (native)\n    at inspectObj (_funcs1:6:25)\n    at _funcs1:8:22\n    at Array.some (native)\n    at inspectObj (_funcs1:6:35)\n    at _funcs1:8:22\n    at Array.some (native)\n    at inspectObj (_funcs1:6:35)\n    at _funcs1 (_funcs1:14:16) near 'rn Object.keys(doc).some(function(key) '  (line 6)",
    "code" : 16722
}

To reproduce the above JSON, use the following code:

use spt

db.coll.drop()

db.coll.insert([
  {settings: {arr: ["a", "b"]}, user: "userA"},
  {settings: {arr: ["c", "d"]}, user: "userB"},
  {settings: {arr: ["e", "f"]}, user: "userC"},
  {settings: {arr: ["g", "g"]}, user: "userD"},
])

db.coll.ensureIndex({user: 1})

db.setProfilingLevel(0)
db.system.profile.drop()
db.setProfilingLevel(2)


db.coll.find({user: "userC"}, {"settings.arr": 1}).limit(1).pretty()
db.system.profile.find().pretty()

Solution

  • The error you mention is thrown by Object.keys when the object is not an enumerable. An easy way is to catch and ignore the error:

    db.system.profile.find(
      // Returns all documents, for which this function returns true.
      function () {
        var findKey = "stage",
            findVal = "COLLSCAN";
    
        function inspectObj(doc) {
          // Object.keys(doc) works only for enumerables like (Arrays and Documents)
          // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
          // The incoming Object might also be e.g. `ISODate("2015-07-29T08:20:42.768Z")`
          // Thus you need to catch (and ignore) TypeErrors thrown by Object.keys()
          var contains = false;
          try {
                contains = Object.keys(doc).some(function(key) {
                  cVal = doc[key];
                  if ( typeof(cVal) == "object" ) {
                    // Recursive call if value is an Object.
                    return inspectObj(doc[key]);
                  } else {
                    return ( key == findKey && doc[key] == findVal );
                  }
                });
          } catch(TypeError) {
            // ignore TypeError
          }
          return contains;
        }
    
        return inspectObj(this);
      }, {op: 1, ns:1, query: 1}
    )