gpt4 book ai didi

node.js - Mongodb 和 Mongoose 未创建唯一索引

转载 作者:太空宇宙 更新时间:2023-11-04 02:34:34 24 4
gpt4 key购买 nike

我在互联网上进行了大量搜索,似乎没有人能解决这个问题。我有一个与 Mongo 中的数据库绑定(bind)的 Mongoose 模式。我想在我的用户文档的电子邮件字段上创建唯一索引。据我从我能找到的有限文档中可以看出,下面的代码应该可以工作。谁能告诉我为什么失败,并允许我使用重复的电子邮件创建用户?

var userSchema = new Schema({
email: { type: String, required: true, index: { unique: true, trim: true } },
password: String,
firstName: String,
lastName: String
}, { autoIndex: true });

最佳答案

那个 trim 可能不应该在那里:它是字符串的设置,而不是索引的设置。请尝试以下操作:

var userSchema = new Schema({
email: {
type: String,
trim: true,
required: true,
unique: true
},
password: String,
firstName: String,
lastName: String
}, { autoIndex: true });

关于node.js - Mongodb 和 Mongoose 未创建唯一索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23643207/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com