Last active
November 29, 2017 10:40
-
-
Save fiveisprime/9749782 to your computer and use it in GitHub Desktop.
Replace `_id` with string `id` and remove `__v` when calling `toObject()` in Mongoose.
This file contains 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
if (!MySchema.options.toObject) { | |
PlanSchema.options.toObject = {}; | |
} | |
MySchema.options.toObject.transform = function(doc, ret) { | |
// Set the id from the retrun object value which will be a string. | |
ret.id = ret._id; | |
delete ret._id; | |
delete ret.__v; | |
}; | |
// The resulting `id` field will be something like "532263ad59a6316d59000004". |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment