mongoosefindOne与'任一或'查询
我有一个Mongo用户数据库,我用Mongoose查询。 我想做findOne来确定用户是否已经存在。 我希望它首先search用户是否已经存在一个电子邮件,如果它不,它应该然后search,看看用户是否存在与电话。 这是否必须在两个单独的查询中完成,还是可以合并为一个?
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! });
为什么不使用$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. });
这是伪SQL
等价物:
SELECT * FROM users WHERE email = '%1' OR phone = '%2'
- CommonJs模块系统中“module.exports”和“exports”的区别
- 如何hook / monkey-patch基本的Error类
- 在Node.js中inputpincode值需要获取国家,州,城市和地区
- 快递 – 发送页面和自定义数据到一个单一的请求浏览器?
- 等待Node.js表示应用程序调用http.request的for循环
- WebSocket连接到'ws:// localhost:3000 /'失败:连接在收到握手响应之前closures
- 怎么过程.__ proto__!== process.constructor.prototype?
- Node-XMPP stringprep错误
- 使用多个连接创build一个书架关系模型