2017-08-09 63 views
0

我需要使用Node.js通過單個MongoDB查詢來查找集合數據。我正在解釋下面的集合。使用Node.js在單個mongoDB查詢中找不到數據

f_user_login:

{ 
    "_id": { 
     "$oid": "5981b48654d471000459208e" 
    }, 
    "email": "[email protected]", 
    "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": "[email protected]", 
    "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;//[email protected] 
    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

難道你不應該做'getdocs [1]'? '.find()'方法傳遞所有匹配的數組,所以如果你需要_second_匹配,我想你會想要第一個索引。 – spicypumpkin

回答

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); 
    } 
    }) 
} 
+0

不,如果電子郵件/密碼錯誤。在這裏我需要三種不同的信息。 – satya

+0

如果電子郵件和密碼錯誤,您的getdocs將爲空。這就是爲什麼put/*檢查getdocs是否爲空* /。如果沒有解決,我會鑽更多。 –