Mongo在非唯一字段上给出“重复键错误" [英] Mongo Giving 'duplicate key error' on non-unique fields

查看:23
本文介绍了Mongo在非唯一字段上给出“重复键错误"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试插入子文档时遇到 MongoDB 错误.子文档已经有唯一的 _id,但是我不希望唯一的不同的非唯一字段引发错误.

I am getting a MongoDB error when trying to insert a subdocument. The subdocs already have unique _ids, but an error is being thrown for a different, non-unique field that I don't want unique.

Angular 中的错误是:Assets.serial 已经存在".如何使该字段包含重复值,以及是什么导致模型假定它应该是唯一的?

The error in Angular is: "Assets.serial already exist". How can I make this field contain duplicate values, and what is causing the model to assume it should be unique?

这是我的猫鼬模型:

'use strict';

var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var AssetUrlSchema = new Schema({
  name: {
    type: String,
    unique: false,
    default: '',
    trim: true
  },
  url: {
    type: String,
    unique: false,
    default: 'http://placehold.it/75x75',
    trim: true
  },
}),

AssetSchema = new Schema({
  serial: {
    type: Number,
    unique: false
  },
  urls: {
    type: [AssetUrlSchema],
    unique: false,
    default: [
      { name: '', url: 'http://placehold.it/75x75' },
      { name: '', url: 'http://placehold.it/75x75' }
    ]
  }
}),

/**
 * Item Schema
 */
ItemSchema = new Schema({
    name: {
        type: String,
        default: '',
        required: 'Please enter name',
        trim: true
    },

  assets: {
    type: [AssetSchema],
    default: [],
    unique: false
  },

  property: {
    type: Schema.ObjectId,
    zd: 'Please select a property',
    ref: 'Property'
  },

    created: {
        type: Date,
        default: Date.now
    },

    user: {
        type: Schema.ObjectId,
        ref: 'User'
    }
});

mongoose.model('Item', ItemSchema);

这是我的保存"方法:

function(){
      var i = 0, assets = [];

      for (;i < 24;i++) {
        assets.push({
          serial: 1000+i,
          urls: {
            name: 'Asset Name ' + i,
            url: 'http://placehold.it/75x75?'
          }
        });
      }

      item = new Items ({
        name: 'FPO',
        property: newPropId,
        assets: assets
      });

      return item.$save(
        function(response){ return response; },
        function(errorResponse) {
          $scope.error = errorResponse.data.message;
        }
      );
    }

我第一次插入文档时,它工作正常.随后的任何时候,它都会失败并显示 400,因为 assets.serial 字段不是唯一的.但是,我特别将该字段标记为 unique:false.

The first time I insert a document, it works fine. Any subsequent time, it fails with a 400 because the assets.serial field is not unique. However, I am specifically marking that field as unique:false.

模式控制台中的错误是:

The error in the mode console is:

{ [MongoError: insertDocument :: caused by :: 11000 E11000 duplicate key error index: mean-dev.items.$assets.serial_1  dup key: { : 1000 }]
name: 'MongoError',
code: 11000,
err: 'insertDocument :: caused by :: 11000 E11000 duplicate key error index: mean-dev.items.$assets.serial_1  dup key: { : 1000 }' }
POST /api/items 400 14.347 ms - 41

推荐答案

Mongoose 不会删除现有索引,因此您需要显式删除索引以摆脱它.在外壳中:

Mongoose doesn't remove existing indexes so you'll need to explicitly drop the index to get rid of it. In the shell:

> db.items.dropIndex('assets.serial_1')

如果您最初定义该字段 unique: true 但随后将其从架构定义中删除或将其更改为 unique: false,则会发生这种情况.

This will happen if you initially define that field unique: true but then later remove that from the schema definition or change it to unique: false.

这篇关于Mongo在非唯一字段上给出“重复键错误"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆