Node.js自动增量E11000重复键错误收集

时间:2018-10-27 20:25:54

标签: node.js mongoose mongoose-schema mongoose-auto-increment

我正在为错误E11000重复键错误集合苦苦挣扎,我有一个带有另一个子模式数组的架构,当我尝试用空数组插入我的架构时,我总是收到此错误。我试图在未成功保存的情况下将其设置为undefined ...我已经从mongoDB及其索引中删除了我的方案。我在标记模式中插入“自动增量”后,就会出现错误。

城市架构:

            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);

标记架构

            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);

3 个答案:

答案 0 :(得分:0)

尝试一下:

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

我认为该错误是因为您尚未定义自动增量应在其中起作用的初始值,并且未将其声明为唯一的事实?

答案 1 :(得分:0)

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

答案 2 :(得分:0)

我知道这是一个老问题,但是对于未来的观众来说,这就是我的工作。

我在插件架构上添加了一种数字,并将unique设置为false,以避免在操作过程中出现重复。

所以新的插件模式将是

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

citySchema.plugin(autoIncrement.plugin, {
     model: 'city',
     field: 'id',
     startAt: 1,
     incrementBy: 1,
     type: Number,
     unique: false
});
相关问题