【发布时间】:2021-09-28 18:25:27
【问题描述】:
我正在尝试使 kevin@gmail.com 与 KEVIN@gmail.com 相同,这是 User.js 文件。我无法找到使电子邮件和用户名不区分大小写的方法。 @username 应该与@UserName 相同。 我阅读了有关查询here 的信息,但不知道如何将其添加到架构中。我希望请求有任何大小写,但在与数据库中存储的请求进行比较时,不应区分大小写。
const mongoose = require('mongoose');
const UserSchema = new mongoose.Schema(
{
username: {
type: String,
required: true,
min: 3,
max: 15,
unique: true,
index: {
unique: true,
collation: { locale: 'en', strength: 2 },
},
},
email: {
type: String,
required: true,
max: 50,
unique: true,
index: {
unique: true,
collation: { locale: 'en', strength: 2 },
},
},
password: {
type: String,
required: true,
min: 6,
},
profilePicture: {
type: String,
default: '',
},
cover: {
type: String,
default: '',
},
followers: {
type: Array,
default: [],
},
following: {
type: Array,
default: [],
},
isAdmin: {
type: Boolean,
default: false,
},
},
{ timestamps: true }
);
module.exports = mongoose.model('User', UserSchema);
另外,下面的代码有意义吗?
index: {
unique: true,
collation: { locale: 'en', strength: 2 }
【问题讨论】: