我需要使用Node.js通过单个MongoDB查询找到集合数据。我正在解释下面的集合。
f_user_login:
{
"_id": {
"$oid": "5981b48654d471000459208e"
},
"email": "subhrajyoti.pradhan@oditeksolutions.com",
"password": "d0e49bcba946540cb3d5fc808870d16b",
"dob": "02-08-2017",
"created_date": "2017-08-02 11:16:21",
"updated_date": "2017-08-02 11:16:21",
"status": 1,
"token": "b85ff4c47093217587f8c7f2fff7ff86b1bcbf6a7321705871435929ee38",
"verification_id": ""
}
{
"_id": {
"$oid": "598aa189e5f78d00042f48ae"
},
"email": "subhrajyotipradhan@gmail.com",
"password": "d0e49bcba946540cb3d5fc808870d16b",
"dob": "1986-04-10",
"created_date": "2017-08-09 05:45:44",
"updated_date": "2017-08-09 05:45:44",
"status": 0,
"token": "",
"verification_id": "7ffbe3f9be82b2af84491d3e8dff4fa1a65f973d"
}
我正在解释下面的代码。
exports.userSignin=function(req,res){
var email=req.body.email;//subhrajyotipradhan@gmail.com
var password=req.body.password;//d0e49bcba946540cb3d5fc808870d16b
var pass=mden(password);
if (email !='' && password !='') {
db.f_user_login.count({email:email,password:pass,status:1},function(err,docs){
if(docs > 0){
token=crypto.randomBytes(30).toString('hex');
db.f_user_login.update({email:email},{$set:{token:token}},function(err,doc){
db.f_user_login.find({email:email},function(error,docu){
var edata=[{"email": docu[0].email,"dob": docu[0].dob,"created_date":docu[0].created_date ,"id": docu[0]._id,"updated_date":docu[0].updated_date,"token_id":token}];
var data={"statusCode": 200,"data":edata,"message": "The user logged successfully."};
res.send(data);
})
})
}else{
console.log(email,password);
db.f_user_login.find({$or:[{email:email},{password:pass},{status:1}]},function(err,getdocs){
if (!err) {
var uemail=getdocs[0].email;
var upass=getdocs[0].password;
var ustatus=getdocs[0].status;
console.log('email,pass,status',uemail,upass,ustatus);
if (uemail != email) {
var data={"statusCode": 401,"error": "Unauthorized","message": "Invalid email id .Please provide a valid email id"};
}
if (upass != pass) {
var data={"statusCode": 401,"error": "Unauthorized","message": "Invalid Password .Please provide a valid Password"};
}
if (ustatus == 0) {
var data={"statusCode": 401,"error": "Unauthorized","message": "you have not verified your account using the link sent to your email."};
}
res.send(data);
}
})
}
})
}
}
此处我的输入email and password
是正确的,我需要检查状态并获取此you have not verified your account using the link sent to your email.
消息。但不幸的是,两个文件都有相同的密码,它总是检查第一个文件,但最初我需要检查第二个文件进行验证。
可以使用3个三个不同的查询,但在这里我想要操作一个查询。这可能吗?
答案 0 :(得分:0)
您可以在整个过程中使用以下代码:
注意:可能需要更改一些拼写错误或代码。但你可以使用基本的想法。
if (email !='' && password !='') {
console.log(email,password);
db.f_user_login.find({$and:[{email:email},{password:pass}]},function(err,getdocs){
if (!err && /*check if the getdocs is not empty*/) {
var uemail=getdocs[0].email;
var upass=getdocs[0].password;
var ustatus=getdocs[0].status;
// update your collection from here with ID
// Remember update is work fine with id
console.log('email,pass,status',uemail,upass,ustatus);
if (ustatus === 0) {
var data={"statusCode": 401,"error": "Unauthorized","message": "you have not verified your account using the link sent to your email."};
}else if(ustatus === 1){
var data={"statusCode": 200,"data":edata,"message": "The user logged successfully."};
}
res.send(data);
}
})
}