【发布时间】:2014-07-01 19:59:24
【问题描述】:
我在互联网上进行了广泛搜索,但似乎没有人对此问题有答案。我有一个与 Mongo 中的数据库相关联的 Mongoose 模式。我想在我的用户文档的电子邮件字段上创建一个唯一索引。从我能找到的有限文档中可以看出,下面的代码应该可以工作。谁能告诉我为什么会失败,并允许我创建具有重复电子邮件的用户?
var userSchema = new Schema({
email: { type: String, required: true, index: { unique: true, trim: true } },
password: String,
firstName: String,
lastName: String
}, { autoIndex: true });
【问题讨论】:
-
你能在命令行上使用 mongo 进入 mongodb 并检查索引(如果存在的话)是什么样子并将其添加到你的问题中吗?