Created
July 3, 2013 08:39
-
-
Save h2rd/5916403 to your computer and use it in GitHub Desktop.
mongoose add virtual field or tojson
This file contains hidden or 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
| 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