猫鼬模式选项更改不会反映在数据库中 [英] Mongoose schema option change doesn't reflect in database

查看:49
本文介绍了猫鼬模式选项更改不会反映在数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我定义了一个猫鼬模式,如下所示:

I have a mongoose schema defined like the following one:

{
    Username: {
        type: String,
        unique: true,
        required: true,
    },
    Password: {
        type: String,
        required: true,
        minlength: 10,
        maxlength: 20
    }
}

例如,在启动正在运行的应用程序的生产版本之后,如果我想将用户名"的唯一"更改为 unique:false ,我该怎么做?

For example after we launch the production build of our app which is running live, if I want to change "unique" to unique: false for the "Username" how should I do it?

因此,在我的机器上,当服务器运行时,我使用用户名和密码创建了一个用户,mongo为我创建了用户,现在我在代码中手动将Unique Option值更改为 unique:false ,重新启动服务器后,mongo抛出错误,提示"E11000重复密钥错误集合:TFM-Dev-Server.admins索引:Username_1 dup密钥:{用户名:\" admin \}" .但是我确实将Unique选项更改为false.只有当我删除数据库时,它才能工作.但是我不能继续删除带有用户数据的生产数据库,因为这是一个很小的变化.有人请告诉我如何实施最佳方法.

So on my machine when the server is running i created a User with Username and Password, mongo created User for me, now i changed the Unique Option value to unique: false manually in the code and restarted my server, mongo throws an error saying "E11000 duplicate key error collection: TFM-Dev-Server.admins index: Username_1 dup key: { Username: \"admin\" }". But I did change Unique option to false. Only when I drop my database it works. But I can't keep on dropping my production database with User Data for a small change as this. Someone please tell me how to implement the best way.

在我的应用程序中,模式在我的应用程序的几个区域中经常更改,因此我想在不停止&的情况下进行处理删除我的生产服务器的数据库.

In my app Schemas change very often in few areas of my app, I want to deal them without stopping & dropping database of my production server.

谢谢

推荐答案

为什么出现错误:
第一次使用 unique:true 创建架构时,猫鼬会自动为该字段创建一个唯一索引(即 Username ).当您将其从代码内更改为 unique:false 时,更改不会撤消猫鼬所做的工作,唯一索引仍将存在于数据库中.因此,每当您尝试插入另一个具有已经存在的用户名的文档时(即使将唯一选项切换为 false 后),也会出现索引重复键错误.

Why you are having the error:
The first time you created the schema with unique: true, mongoose will auto-create a unique index for the field(i.e Username). When you change it from within your code to unique: false, the change won't undo what mongoose had done, the unique index would still be in the database. So whenever you try to insert another document with a Username that already exists(even after toggling the unique option to false), you will get the index duplicate key error.

解决方案:
首先,您应该知道 unique 选项不是验证或强制 Username 字段唯一的方法,而是它是构建MongoDB唯一索引的助手.来源.
如果您使用 unique 选项进行验证,那么正确的做法是将其删除并实施其他逻辑以确保 Username 字段的唯一性.但是,如果您故意使用 unique 选项自动创建索引,则每当将 unique 选项切换为false时,都需要删除唯一索引.您可以在mongo shell中执行以下操作:

The solution:
Firstly, you should know that the unique option is not a way to validate or enforce the Username field to be unique but rather it's a helper for building MongoDB unique indexes. Source.
If you are using the unique option for validation, then the right thing to do would be to remove it and implement other logic to ensure the uniqueness of the Username field. However, if you are deliberate about using the unique option to auto-create indexes, then you need to drop the unique indexes whenever you toggle the unique option to false. You can do that with this in the mongo shell:

db.Admin.dropIndex( { "Username" : -1 } )

这篇关于猫鼬模式选项更改不会反映在数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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