Skip to content

Instantly share code, notes, and snippets.

@mpobrien
Created April 12, 2016 17:04
Show Gist options
  • Save mpobrien/6c96116c12c5f4118740b1be95da2201 to your computer and use it in GitHub Desktop.
Save mpobrien/6c96116c12c5f4118740b1be95da2201 to your computer and use it in GitHub Desktop.
extjson converter in js for mongo shell
function convertExtJson(o){
// if it's an array, loop over each item
if(o.constructor == Array){
out = []
for(var i=0; i<o.length;i++){
out.push(convertExtJson(o[i]))
}
return out
}else if(o.constructor == Object){
// if it's an object, replace each key one by one
numKeys = Object.keys(o).length
if(numKeys == 1){
if("$date" in o){
return ISODate(convertExtJson(o["$date"]))
}else if("$numberLong" in o){
return NumberLong(o["$numberLong"])
}else if("$code" in o){
// TODO
}else if("$numberInt" in o){
return NumberInt(o["$numberInt"])
}else if("$undefined" in o){
return undefined
}else if("$maxKey" in o){
return MaxKey()
}else if("$minKey" in o){
return MinKey()
}
}
for(var k in o){
if (!o.hasOwnProperty(k)) {
continue
}
o[k] = convertExtJson(o[k])
}
return o
}else{
return o
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment