Commit 5aa8e065 by Wes Freeman

Adding maxDepth functionality.

parent a8e6ed88
...@@ -57,6 +57,35 @@ We are only examining the last document here ("limit = 1"). It belongs to Genevi ...@@ -57,6 +57,35 @@ We are only examining the last document here ("limit = 1"). It belongs to Genevi
But how can totalOccurrences still reach 4? "limit" specifies how many documents to search for keys. Then, the tool calculates totalOccurrences and percentContaining from _all_ the collection's documents, even those outside the "limit". This tradeoff is meant to give the most bang for our buck, when using "limit" and learning about a collection. But how can totalOccurrences still reach 4? "limit" specifies how many documents to search for keys. Then, the tool calculates totalOccurrences and percentContaining from _all_ the collection's documents, even those outside the "limit". This tradeoff is meant to give the most bang for our buck, when using "limit" and learning about a collection.
### Analyze Documents to a Maximum Depth
Perhaps you have a potentially very deep nested object structure, and you don't want to see more than a few levels deep in the analysis.
One can apply a "maxDepth" constraint, which limits the depth variety will recursively search to find new objects.
db.users.insert({name:"Walter", someNestedObject:{a:{b:{c:{d:{e:1}}}}}});
The default will traverse all the way to the bottom of that structure:
$ mongo test --eval "var collection = 'users'" variety.js
...
{ "_id" : { "key" : "someNestedObject" }, "value" : { "types" : [ "Object" ] }, "totalOccurrences" : 2, "percentContaining" : 33.33333333333333 }
{ "_id" : { "key" : "someNestedObject.a" }, "value" : { "types" : [ "Object" ] }, "totalOccurrences" : 2, "percentContaining" : 33.33333333333333 }
{ "_id" : { "key" : "someNestedObject.a.b" }, "value" : { "types" : [ "Object" ] }, "totalOccurrences" : 2, "percentContaining" : 33.33333333333333 }
{ "_id" : { "key" : "someNestedObject.a.b.c" }, "value" : { "types" : [ "Object" ] }, "totalOccurrences" : 2, "percentContaining" : 33.33333333333333 }
{ "_id" : { "key" : "someNestedObject.a.b.c.d" }, "value" : { "types" : [ "Object" ] }, "totalOccurrences" : 2, "percentContaining" : 33.33333333333333 }
{ "_id" : { "key" : "someNestedObject.a.b.c.d.e" }, "value" : { "types" : [ "Number" ] }, "totalOccurrences" : 2, "percentContaining" : 33.33333333333333 }
$ mongo test --eval "var collection = 'users', maxDepth = 3" variety.js
...
{ "_id" : { "key" : "someNestedObject" }, "value" : { "types" : [ "Object" ] }, "totalOccurrences" : 2, "percentContaining" : 33.33333333333333 }
{ "_id" : { "key" : "someNestedObject.a" }, "value" : { "types" : [ "Object" ] }, "totalOccurrences" : 2, "percentContaining" : 33.33333333333333 }
{ "_id" : { "key" : "someNestedObject.a.b" }, "value" : { "types" : [ "Object" ] }, "totalOccurrences" : 2, "percentContaining" : 33.33333333333333 }
As you can see, variety only traversed three levels deep.
##### "But my dad told me MongoDB is a schemaless database!" ##### ##### "But my dad told me MongoDB is a schemaless database!" #####
First of all, your father is a great guy. Moving on... First of all, your father is a great guy. Moving on...
......
...@@ -19,6 +19,9 @@ if (typeof collection === "undefined") { ...@@ -19,6 +19,9 @@ if (typeof collection === "undefined") {
if (typeof limit === "undefined") { var limit = db[collection].count(); } if (typeof limit === "undefined") { var limit = db[collection].count(); }
print("Using limit of " + limit); print("Using limit of " + limit);
if (typeof maxDepth === "undefined") { var maxDepth = 99; }
print("Using maxDepth of " + maxDepth);
varietyCanHaveChildren = function (v) { varietyCanHaveChildren = function (v) {
var isArray = v && var isArray = v &&
typeof v === 'object' && typeof v === 'object' &&
...@@ -32,7 +35,7 @@ varietyCanHaveChildren = function (v) { ...@@ -32,7 +35,7 @@ varietyCanHaveChildren = function (v) {
} }
db.system.js.save( { _id : "varietyCanHaveChildren", value : varietyCanHaveChildren } ); db.system.js.save( { _id : "varietyCanHaveChildren", value : varietyCanHaveChildren } );
varietyMapRecursive = function(parentKey, keys) { varietyMapRecursive = function(parentKey, keys, level) {
for (var key in keys) { for (var key in keys) {
var value = keys[key]; var value = keys[key];
...@@ -40,8 +43,8 @@ varietyMapRecursive = function(parentKey, keys) { ...@@ -40,8 +43,8 @@ varietyMapRecursive = function(parentKey, keys) {
emit({key : key}, {type: varietyTypeOf(value)}); emit({key : key}, {type: varietyTypeOf(value)});
if (varietyCanHaveChildren(value)) { if (level < maxDepth - 1 && varietyCanHaveChildren(value)) {
varietyMapRecursive(key, value); varietyMapRecursive(key, value, level + 1);
} }
} }
} }
...@@ -91,7 +94,7 @@ map = function() { ...@@ -91,7 +94,7 @@ map = function() {
emit({key : key}, {type: varietyTypeOf(value)}); emit({key : key}, {type: varietyTypeOf(value)});
if (varietyCanHaveChildren(value)) { if (varietyCanHaveChildren(value)) {
varietyMapRecursive(key, value); varietyMapRecursive(key, value, 1);
} }
} }
} }
...@@ -117,7 +120,7 @@ db[collection].mapReduce(map, reduce, { ...@@ -117,7 +120,7 @@ db[collection].mapReduce(map, reduce, {
db : "varietyResults"}, db : "varietyResults"},
limit : limit, limit : limit,
sort : {_id: -1}, sort : {_id: -1},
scope : { limit : limit }}); scope : { limit : limit, maxDepth:maxDepth }});
var resultsDB = db.getMongo().getDB("varietyResults"); var resultsDB = db.getMongo().getDB("varietyResults");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment