Skip to content

Instantly share code, notes, and snippets.

@warlockdn
Created April 3, 2018 21:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save warlockdn/974a74147df1eb36ffdf9456ed7db6f1 to your computer and use it in GitHub Desktop.
Save warlockdn/974a74147df1eb36ffdf9456ed7db6f1 to your computer and use it in GitHub Desktop.
Mongoose Index Error
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