Skip to content

Instantly share code, notes, and snippets.

@fiveisprime
Last active November 29, 2017 10:40
Show Gist options
  • Save fiveisprime/9749782 to your computer and use it in GitHub Desktop.
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.
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