Skip to content

Instantly share code, notes, and snippets.

@h2rd
Created July 3, 2013 08:39
Show Gist options
  • Select an option

  • Save h2rd/5916403 to your computer and use it in GitHub Desktop.

Select an option

Save h2rd/5916403 to your computer and use it in GitHub Desktop.
mongoose add virtual field or tojson
Schema.virtual('id').get(function(){
return this._id.toHexString();
});
Schema.set('toJSON', {
virtuals: true
});
// or
Schema.methods.toJSON = function(options) {
var document = this.toObject(options);
document.id = document._id.toHexString();
delete(document._id);
return document;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment