How do I search MongoDB documents at different nesting levels?
I am currently parsing a system.profile
MongoDB database -Collection. I would like to find "Requests" that have a stage with COLLSCAN
oder IXSCAN
. My problem is that the field stage
can occur at multiple levels ( ...
: shorthand 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 example above, the stage occurs at three levels:
-
"execStats.stage"
-
"execStats.inputStage.stage"
-
"execStats.inputStage.inputStage.stage"
There can be even deeper nested stages. Is there a way to return all documents with stage: "IXSCAN"
oder stage: "COLLSCAN"
at any of the nesting levels? or do I need to run a query for each nesting level?
I tried to use the following function related to How to find MongoDB field name at arbitrary depth , but unfortunately it 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 messages:
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()
source to share
You mentioned the error mentioned Object.keys
when the object is not 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}
)
source to share