Coo*_*oop 10 javascript mongoose mongodb node.js
我有一个Mongo用户数据库,我正在查询Mongoose.我想做findOne来确定用户是否已经存在.我希望它首先搜索用户是否已经存在电子邮件,如果用户不存在,则应搜索用户是否存在电话.这是否必须在2个单独的查询中完成,还是可以归为一个?
User.findOne({ email: req.body.email }).exec(function(err, user){
if (user) //user already exists with email
else //no users with that email but we haven't checked phone number yet!
});
Run Code Online (Sandbox Code Playgroud)
And*_*nai 33
为什么不使用$or运营商?
User.findOne({$or: [
{email: req.body.email},
{phone: req.body.phone}
]}).exec(function(err, user){
if (user) {} //user already exists with email AND/OR phone.
else {} //no users with that email NOR phone exist.
});
Run Code Online (Sandbox Code Playgroud)
这是伪SQL等价物:
SELECT * FROM users WHERE email = '%1' OR phone = '%2'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8466 次 |
| 最近记录: |