const uniqueValidator = require('mongoose-unique-validator'); module.exports = mongoose => { var schema = mongoose.Schema( { from_type: String, from: String, to_type: String, to: String, department: String, content: String, status: String, create_by: String, create_date: Date, site: Number }, { collection: 'message', timestamps: true } ); schema.method("toJSON", function() { const { __v, _id, ...object } = this.toObject(); object.id = _id; return object; }); schema.plugin(uniqueValidator); const SentMessage = mongoose.model("sentMessage", schema); return SentMessage; };