SELECT *
FROM user
WHERE (
id_1 = '$entity_id' OR
id_2 = '$entity_id' OR
id_3 = '$entity_id' OR
id_4 = '$entity_id'
) AND
token = '$token'
我有一个名为“用户”的模型。我需要检查用户是否拥有提供的令牌,以及至少有一个id_ *与$ entity_id匹配。
我试过这个,但没有用:
mongoose.model('User').find(
{ 'local.token': req.query.token },
{$or: [
{ "local._dormitory": req.params.id },
{ "local._building": req.params.id },
{ "local._hall": req.params.id },
{ "local._room": req.params.id }
]}, {}, function(err, doc){
if(err) {
console.log(err);
return false;
}
if(doc != null) {
console.log("Scope access exists");
console.log(doc);
return true;
// res.json(200, { message: doc, Response: "true" });
} else {
console.log("Scope access doesn't exist");
return false;
// res.json(401, { message: "Auth failed" });
}
});
答案 0 :(得分:1)
您需要将查询的两个部分放在同一个查询对象中:
mongoose.model('User').find(
{ 'local.token': req.query.token,
$or: [
{ "local._dormitory": req.params.id },
{ "local._building": req.params.id },
{ "local._hall": req.params.id },
{ "local._room": req.params.id }
]}, {}, function(err, doc){
...