MongooseJS 不能禁用唯一的字段 [英] MongooseJS cant disable unique to field

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

问题描述

这是我的 mongoosejs 架构.我将 name unique 设置为 false,但这就是我得到的:MongoError: insertDocument :: 由 :: 11000 E11000 重复键错误索引:testdb1.images.$name_1 dup key: { : "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: []

});

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

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 不能禁用唯一的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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