2017-08-09 5 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.メッセージを取得する必要があります。しかし残念なことに、両方のドキュメントは常に最初のドキュメントをチェックするのと同じパスワードを持っていますが、もともとは検証のために2番目のドキュメントをチェックする必要があります。

3つの異なるクエリを使用することは可能ですが、ここでは1つのクエリを操作したいと考えています。これは可能ですか?

+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

いいえ、メール/パスワードが間違っている場合。ここで私は3つの異なるメッセージが必要です。 – satya

+0

電子メールとパスワードが間違っていると、getdocsは空になります。 getdocsが空でないかどうかを確認する*//です。解決されなければ、私はそれにもっと掘削します。 –