Skip to content

Instantly share code, notes, and snippets.

@codeboost
Created December 2, 2011 19:17
Show Gist options
  • Save codeboost/1424465 to your computer and use it in GitHub Desktop.
Save codeboost/1424465 to your computer and use it in GitHub Desktop.
Convert Mongoose obj to JS obj and all ObjectIds to String
toObject = (item) ->
return item unless item.toObject
json = item.toObject()
for key, val of json
if _.isArray val
json[key] = _.map val, (element) -> toObject element
else
if val.toHexString then json[key] = val.toHexString()
return json
jsonify = (item) ->
return item unless item
if _.isArray item
_.map item, (element) -> toObject element
else
toObject(item)
@codeboost
Copy link
Author

This sample use toHexString to detect if an object is a ObjectID.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment