0

I'm struggling with Error E11000 duplicate key error collection, I have a schema with another sub-schema array and when I try to insert my schema with the empty array I allways get this error. I tried to set it undefined on pre-save without sucesss... I have deleted my schem from mongoDB and its indexes. The error appears after I insted **autoincrement **in Marker schema.

City Schema:

            let mongoose = require('mongoose');
            let autoIncrement = require('mongoose-auto-increment');
            let Marker = require('./marker');

            var MarkerSchema = require('mongoose').model('marker').schema;

            //City Schema
            //status : 1 Ok, 2 Hidden, 3 Deleted


            let citySchema = mongoose.Schema({
               id: {
                type: Number,
                required: true
              },
              name: {
                type: String,
                required: true
              },
              status: {
                type: Number,
                required: true
              },

              coordinates: {
                latitude: {
                  type: Number,
                  required: true
                },
                longitude: {
                  type: Number,
                  required: true
                }
              },
              markers: [MarkerSchema]

            });

            citySchema.pre('save', function (cb) {
              console.log('pre save');
              if (this.markers && this.markers.length === 0) {
              this.markers = undefined;
              }
              cb();
            });

            citySchema.plugin(autoIncrement.plugin, {
                model: 'city',
                field: 'id',
                startAt: 1,
                incrementBy: 1
            });


            let City = module.exports = mongoose.model('city', citySchema);

marker schema

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

            let markerSchema = mongoose.Schema({
                status: {
                    type: Number,
                    required: true
                  },
                description: {
                  type: String,
                  required: true
                },
                coordinates: {
                    latitude: {
                      type: Number,
                      required: true
                    },
                    longitude: {
                      type: Number,
                      required: true
                    }
                }
            });

            markerSchema.plugin(autoIncrement.plugin, {
                model: 'marker',
                field: 'id',
                startAt: 1,
                incrementBy: 1
            });

            let Marker = module.exports = mongoose.model('marker', markerSchema);
John
  • 1,697
  • 4
  • 27
  • 53

3 Answers3

1

Having the same issue ... Rather sure this is caused by the mongoose-auto-increment module...

Johnny Driesen
  • 151
  • 1
  • 4
0

try this:

let citySchema = new mongoose.Schema({
               id: {type: Number, default: 0, unique: true},
....

I think the error is because you have not defined an initial value where the auto-increment should work and the fact that it is not declared as unique ?

  • Thank you for your help, I tried that without success. The error is on markers array , I added those properties to marker schema as well without sucess. – John Oct 28 '18 at 10:17
  • You're right, I did not read well, but now paying more attention, you want to auto-increase from 1 to 1, starting from 1? but by default already increase 1 in 1, and if you just delete " incrementBy: 1 " Or just let it start from 0 the counter, or does this affect? markerSchema.plugin(autoIncrement.plugin, 'marker') --> this starts from 0 and increases by 1 in 1 – Samuel Leiton Oct 28 '18 at 15:40
0

I know this is an old question but for future viewers here is what i did.

I added a type of number on the plugin schema and set unique to false to avoid duplication during manipulation.

so the new plugin schemas will be

markerSchema.plugin(autoIncrement.plugin, {
     model: 'marker',
     field: 'id',
     startAt: 1,
     incrementBy: 1,
     type: Number,
     unique: false
});

and

citySchema.plugin(autoIncrement.plugin, {
     model: 'city',
     field: 'id',
     startAt: 1,
     incrementBy: 1,
     type: Number,
     unique: false
});
Mark
  • 51
  • 6