3

I am trying to increment the value of procedureid & doctorId whenever the api call is being fired. I am using the below syntax. However, it's giving an error Treatment.procedureid_1 dup key when the second time I call the api through browser. i could see that values of procedureid and doctorId are increasing in identityCounter collection created by auto increment.However same is not reflected in respective schema

Can you please help me to resolve the issue.

 var mongoose = require('mongoose');   
 var autoIncrement = require('mongoose-auto-increment');

 var hospitalDoctorSchema = new Schema({


    Treatment: {
                procedureid: { type: Number,required: true, unique: true,default: 0 },               

                doctor: {
                    doctorId: { type: Number, required: true, unique: true, dropDups: true, default: 0 },

                },
    },
    updated_at: { type: Date, required: true, default: Date.now }
});


autoIncrement.initialize(mongoose.connection);

hospitalDoctorSchema.plugin(autoIncrement.plugin, {
    model: collection,
    field: 'Treatment.procedureid',
    startAt: 10000,
    incrementBy: 1
});
hospitalDoctorSchema.plugin(autoIncrement.plugin, {
    model: collection,
    field: 'Treatment.doctor.doctorId',
    startAt: 10000,
    incrementBy: 1
});

//create collection.
module.exports.hospitalModel = mongoose.model(collection, hospitalDoctorSchema);
Sona Shetty
  • 997
  • 3
  • 18
  • 41
  • Are you expecting a new procedureId and doctorId when you create a new hospitalDoctor document? The error you are seeing is likely coming from an index created by auto-increment – Daniel Flippance Aug 01 '17 at 18:54

0 Answers0