如何使用 mongoose 在 mongoDb 中编写 case 语句
How to write case statement in mongoDb using mongoose
我在这里使用 mongoose 作为我的 Mean 应用程序,它由 nodejs、expressjs 和 angularjs.
组成
我的问题是我需要使用不同的查找条件并根据发送错误消息或数据
现在我使用如下
User.findOne({ Email: req.body.Email }, (err, user) => {
if (err) { return done(err); }
if (!user) {
return res.json("Account with that email address does not exist.");
}
猫鼬模式
const userSchema = new mongoose.Schema({
FirstName: String,
LastName: String,
Email: { type: String, unique: true },
UserName: String,
Password: String,
PhoneNumber: String,
EmailVerified : Boolean,
AdminApproved : Boolean
}, { versionKey: false });
但现在我有电子邮件验证和管理员批准等字段。
在 Ms-Sql 中,我使用了像
这样的 case 语句
- 其中(电子邮件不存在)然后
show Email not exist
- 其中(存在电子邮件但未验证电子邮件)然后
show please verify email
- 其中(存在电子邮件,电子邮件已验证且未经管理员批准)然后
show wait for admin process
谁能帮我 mongoDB
使用 mongoose
如何实现相同的操作。
到目前为止你已经做得很好了,你只需要再做一些检查。
试试这个:
User.findOne({ Email: req.body.Email }, (err, user) => {
if (err) { return done(err); }
else if (!user) {
return res.json("Account with that email address does not exist.");
}
else if(!user.EmailVerified){
return res.json("Please verify your email");
}
else if(!user.AdminApproved){
return res.json("Wait for admin process, Not Admin approved yet!");
}
})
我在这里使用 mongoose 作为我的 Mean 应用程序,它由 nodejs、expressjs 和 angularjs.
组成我的问题是我需要使用不同的查找条件并根据发送错误消息或数据
现在我使用如下
User.findOne({ Email: req.body.Email }, (err, user) => {
if (err) { return done(err); }
if (!user) {
return res.json("Account with that email address does not exist.");
}
猫鼬模式
const userSchema = new mongoose.Schema({
FirstName: String,
LastName: String,
Email: { type: String, unique: true },
UserName: String,
Password: String,
PhoneNumber: String,
EmailVerified : Boolean,
AdminApproved : Boolean
}, { versionKey: false });
但现在我有电子邮件验证和管理员批准等字段。
在 Ms-Sql 中,我使用了像
这样的 case 语句- 其中(电子邮件不存在)然后
show Email not exist
- 其中(存在电子邮件但未验证电子邮件)然后
show please verify email
- 其中(存在电子邮件,电子邮件已验证且未经管理员批准)然后
show wait for admin process
谁能帮我 mongoDB
使用 mongoose
如何实现相同的操作。
到目前为止你已经做得很好了,你只需要再做一些检查。
试试这个:
User.findOne({ Email: req.body.Email }, (err, user) => {
if (err) { return done(err); }
else if (!user) {
return res.json("Account with that email address does not exist.");
}
else if(!user.EmailVerified){
return res.json("Please verify your email");
}
else if(!user.AdminApproved){
return res.json("Wait for admin process, Not Admin approved yet!");
}
})