-
-
Save warlockdn/974a74147df1eb36ffdf9456ed7db6f1 to your computer and use it in GitHub Desktop.
Mongoose Index Error
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
const mongoose = require('mongoose'); // v5.0.11, MongoDB - 3.4.7 | |
const Schema = mongoose.Schema; | |
const autoIncrement = require('mongoose-auto-increment'); | |
mongoose.set('debug', true); | |
// Initialize Auto Increment | |
const connection = mongoose.createConnection("mongodb://localhost:27017/food"); | |
autoIncrement.initialize(connection); | |
const partnerSchema = new Schema({ | |
partnerID: { type: Number, index: true, unique: true, required: true, default: 12000 }, | |
name: { type: String, required: true }, | |
email: { type: String, index: true, unique: true, required: true, trim: true, lowercase: true, }, | |
phone: { type: Number, index: true, unique: true, required: true }, | |
createdOn: { type: Date, default: Date.now }, | |
updatedOn: { type: Date, default: Date.now }, | |
isActive: { | |
type: Boolean, | |
default: false | |
}, | |
isPending: { | |
type: Boolean, | |
default: true | |
} | |
}); | |
const Partner = mongoose.model('Partner', partnerSchema); | |
partnerSchema.plugin(autoIncrement.plugin, { | |
model: 'Partner', | |
field: 'partnerID', | |
startAt: 12000, | |
incrementBy: 3 | |
}); | |
partnerSchema.pre('update', function (next, done) { | |
this.updatedOn = Date.now(); | |
next(); | |
}); | |
partnerSchema.pre('save', function (next, done) { | |
this.createdOn = Date.now(); | |
next(); | |
}); | |
module.exports = Partner; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment