Skip to content

Instantly share code, notes, and snippets.

@tily
Created October 7, 2011 13:40
Show Gist options
  • Save tily/1270304 to your computer and use it in GitHub Desktop.
Save tily/1270304 to your computer and use it in GitHub Desktop.
MongoDB DBQuery extension for easier printjson
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