猫鼬查找多个文件
mongoose find multiple documents
我有一个基本的 User
模式
var UserSchema = new Schema({
name: {
type: String,
required: true
},
password: {
type: String,
required: true
}
});
现在我想实现 dual login
功能,
async.parallel([
function (cb) {
User.findOne({$and: [{name: req.body.username1}, {password: req.body.password1}]}, function (err, u) {
if (!u)
err = "User1 dose not exist";
cb(err, u)
});
},
function (cb) {
User.findOne({$and: [{name: req.body.username2}, {password: req.body.password2}]}, function (err, u) {
if (!u)
err = "User2 dose not exist";
cb(err, u)
});
}
], function (err, results) {
....
我想知道是否有简单的方法可以在一个 User.find()
函数中找到这两个用户信息?
你只用$or
. Also your use of $and
这里是多余的:
User.find({
"$or": [
{ "name": req.body.username1, "password": req.body.password1 },
{ "name": req.body.username2, "password": req.body.password2 }
]
},function(err,result) {
// logic in here
})
然后处理验证响应所需的任何逻辑,最明显的情况是,如果响应的长度不是至少两个项目,则找不到其中一个选择。
这是 "username" 当然应该具有唯一约束的情况之一。
我有一个基本的 User
模式
var UserSchema = new Schema({
name: {
type: String,
required: true
},
password: {
type: String,
required: true
}
});
现在我想实现 dual login
功能,
async.parallel([
function (cb) {
User.findOne({$and: [{name: req.body.username1}, {password: req.body.password1}]}, function (err, u) {
if (!u)
err = "User1 dose not exist";
cb(err, u)
});
},
function (cb) {
User.findOne({$and: [{name: req.body.username2}, {password: req.body.password2}]}, function (err, u) {
if (!u)
err = "User2 dose not exist";
cb(err, u)
});
}
], function (err, results) {
....
我想知道是否有简单的方法可以在一个 User.find()
函数中找到这两个用户信息?
你只用$or
. Also your use of $and
这里是多余的:
User.find({
"$or": [
{ "name": req.body.username1, "password": req.body.password1 },
{ "name": req.body.username2, "password": req.body.password2 }
]
},function(err,result) {
// logic in here
})
然后处理验证响应所需的任何逻辑,最明显的情况是,如果响应的长度不是至少两个项目,则找不到其中一个选择。
这是 "username" 当然应该具有唯一约束的情况之一。