Created
October 7, 2011 13:40
-
-
Save tily/1270304 to your computer and use it in GitHub Desktop.
MongoDB DBQuery extension for easier printjson
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DBQuery.prototype.p = function() { | |
var args = arguments | |
this.forEach(function(e) { | |
var result = {} | |
if(args.length == 0) { | |
result = e | |
} else { | |
for(var i = 0; i < args.length; i++) { | |
var target = e | |
var keys = args[i].split('.') | |
var flag = true | |
keys.forEach(function(key) { | |
if(key in target) { | |
target = target[key] | |
} else { | |
flag = false | |
return | |
} | |
}) | |
if(!flag) { | |
target = '(undefined)' | |
} | |
if(args.length == 1) { | |
result = target | |
} else { | |
result[[args[i]] = target | |
} | |
} | |
} | |
printjson(result) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment