Skip to content

Instantly share code, notes, and snippets.

@shalaby
Forked from cadebward/service.js
Created May 20, 2021 15:47
Show Gist options
  • Save shalaby/bf53d2bc2c57ecd6fe3d7775b952540d to your computer and use it in GitHub Desktop.
Save shalaby/bf53d2bc2c57ecd6fe3d7775b952540d to your computer and use it in GitHub Desktop.
Remove `__v` and `_id` from MongoDB
import mongoose, {Schema} from 'mongoose'
export const ServiceSchema = new Schema({
displayName: {type: String, required: true, unique: true}
})
ServiceSchema.set('toObject', {
transform: function (doc, ret) {
ret.id = ret._id
delete ret._id
delete ret.__v
}
})
export default mongoose.model('Service', ServiceSchema)
// inspired by https://gist.github.com/fiveisprime/9749782
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment