Commit cecdd900 by James Cropcho

Merge pull request #7 from wfreeman/master

Check for Date, ObjectId, BinData explicitly. Remove blacklist of keys.
parents 62f56de5 1b712b00
...@@ -6,19 +6,22 @@ This lightweight tool helps you get a sense of your application's schema, as wel ...@@ -6,19 +6,22 @@ This lightweight tool helps you get a sense of your application's schema, as wel
We'll make a collection: We'll make a collection:
db.users.insert({name: "Tom", bio: "A nice guy.", pets: ["monkey", "fish"], someWeirdLegacyKey: "I like Ike!"}); db.users.insert({name: "Tom", bio: "A nice guy.", pets: ["monkey", "fish"], someWeirdLegacyKey: "I like Ike!"});
db.users.insert({name: "Dick", bio: "I swordfight."}); db.users.insert({name: "Dick", bio: "I swordfight.", birthday: new Date("1974-03-14")});
db.users.insert({name: "Harry", pets: "egret"}); db.users.insert({name: "Harry", pets: "egret", birthday: new Date("1984-03-14")});
db.users.insert({name: "Geneviève", bio: "Ça va?"}); db.users.insert({name: "Geneviève", bio: "Ça va?"});
db.users.insert({name: "Jim", someBinData: new BinData(2,"1234")});
So, let's see what we've got here: So, let's see what we've got here:
$ mongo test --eval "var collection = 'users'" variety.js $ mongo test --eval "var collection = 'users'" variety.js
{ "_id" : { "key" : "_id" }, "value" : { "types" : [ "object" ] }, "totalOccurrences" : 4, "percentContaining" : 100 } { "_id" : { "key" : "_id" }, "value" : { "types" : [ "objectId" ] }, "totalOccurrences" : 5, "percentContaining" : 100 }
{ "_id" : { "key" : "name" }, "value" : { "types" : [ "string" ] }, "totalOccurrences" : 4, "percentContaining" : 100 } { "_id" : { "key" : "name" }, "value" : { "types" : [ "string" ] }, "totalOccurrences" : 5, "percentContaining" : 100 }
{ "_id" : { "key" : "bio" }, "value" : { "types" : [ "string" ] }, "totalOccurrences" : 3, "percentContaining" : 75 } { "_id" : { "key" : "bio" }, "value" : { "types" : [ "string" ] }, "totalOccurrences" : 3, "percentContaining" : 60 }
{ "_id" : { "key" : "pets" }, "value" : { "types" : [ "string", "array" ] }, "totalOccurrences" : 2, "percentContaining" : 50 } { "_id" : { "key" : "birthday" }, "value" : { "types" : [ "date" ] }, "totalOccurrences" : 2, "percentContaining" : 40 }
{ "_id" : { "key" : "someWeirdLegacyKey" }, "value" : { "type" : "string" }, "totalOccurrences" : 1, "percentContaining" : 25 } { "_id" : { "key" : "pets" }, "value" : { "types" : [ "string", "array" ] }, "totalOccurrences" : 2, "percentContaining" : 40 }
{ "_id" : { "key" : "someBinData" }, "value" : { "type" : "binData" }, "totalOccurrences" : 1, "percentContaining" : 20 }
{ "_id" : { "key" : "someWeirdLegacyKey" }, "value" : { "type" : "string" }, "totalOccurrences" : 1, "percentContaining" : 20 }
_("test" is the database containing the collection we are analyzing.)_ _("test" is the database containing the collection we are analyzing.)_
......
...@@ -25,7 +25,10 @@ varietyCanHaveChildren = function (v) { ...@@ -25,7 +25,10 @@ varietyCanHaveChildren = function (v) {
typeof v.length === 'number' && typeof v.length === 'number' &&
!(v.propertyIsEnumerable('length')); !(v.propertyIsEnumerable('length'));
var isObject = typeof v === 'object'; var isObject = typeof v === 'object';
return isArray || isObject; var specialObject = v instanceof Date ||
v instanceof ObjectId ||
v instanceof BinData;
return !specialObject && (isArray || isObject);
} }
db.system.js.save( { _id : "varietyCanHaveChildren", value : varietyCanHaveChildren } ); db.system.js.save( { _id : "varietyCanHaveChildren", value : varietyCanHaveChildren } );
...@@ -57,6 +60,15 @@ varietyTypeOf = function(thing) { ...@@ -57,6 +60,15 @@ varietyTypeOf = function(thing) {
else if (thing === null) { else if (thing === null) {
return "null"; return "null";
} }
else if (thing instanceof Date) {
return "date";
}
else if (thing instanceof ObjectId) {
return "objectId";
}
else if (thing instanceof BinData) {
return "binData";
}
else { else {
return "object"; return "object";
} }
...@@ -109,10 +121,6 @@ var resultsDB = db.getMongo().getDB("varietyResults"); ...@@ -109,10 +121,6 @@ var resultsDB = db.getMongo().getDB("varietyResults");
var numDocuments = db[collection].count(); var numDocuments = db[collection].count();
// Using our method of retrieving keys, Mongo gets confused about the following, and
// incorrectly thinks they are keys. -JC
var blackListKeys = ["_id.equals", "_id.getTimestamp", "_id.isObjectId", "_id.str","_id.tojson"];
resultsDB[resultsCollectionName].find({}).forEach(function(key) { resultsDB[resultsCollectionName].find({}).forEach(function(key) {
keyName = key["_id"].key; keyName = key["_id"].key;
...@@ -123,17 +131,6 @@ resultsDB[resultsCollectionName].find({}).forEach(function(key) { ...@@ -123,17 +131,6 @@ resultsDB[resultsCollectionName].find({}).forEach(function(key) {
return; return;
} }
var blackListKeyFound = false;
blackListKeys.forEach(function(blackListKey) {
if(keyName === blackListKey) {
resultsDB[resultsCollectionName].remove({ "_id" : { key: keyName }});
blackListKeyFound = true;
}
});
if(blackListKeyFound) { return; }
if(!(keyName.match(/\.XX/) && !keyName.match(/\.XX$/))) { if(!(keyName.match(/\.XX/) && !keyName.match(/\.XX$/))) {
// i.e. "Unless the key's value is an array which contains arrays" -JC // i.e. "Unless the key's value is an array which contains arrays" -JC
// ...we do not support totalOccurrences for these keys because it is // ...we do not support totalOccurrences for these keys because it is
......
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