本文介绍了MongooseJS无法禁用字段唯一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的mongoosejs模式.我将名称唯一设置为false,但这是我得到的: MongoError:insertDocument ::由:: 11000 E11000重复键错误索引引起:testdb1.images.$ name_1 dup键:{:"aaa"}

This is my mongoosejs schema. I set name unique to false, but this is what i get: MongoError: insertDocument :: caused by :: 11000 E11000 duplicate key error index: testdb1.images.$name_1 dup key: { : "aaa" }

imageSchema = new Schema({
    url: {
        type: String,
        unique: true,
        required: true
    },

    category: {
        type: String,
        required: true
    },

    vote: {
        type: Number,
        required: true
    },

    name: {
        type: String,
        unique: false,
        required: true
    },

    voteArray: [],
    favorite: false,
    tags: []

});

任何想法如何解决这个问题?建议?

any ides how to solve this ? suggestions ?

推荐答案

Mongoose不会修改现有索引,因此您需要将该索引删除到MongoDB Shell中,然后让Mongoose使用架构中的定义重新创建它:

Mongoose won't modify existing indexes, so you'll need to drop that index in the MongoDB shell and then let Mongoose recreate it using the definition in your schema:

> db.images.dropIndex('name_1');

这篇关于MongooseJS无法禁用字段唯一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-22 16:44