'How to compare input and Document in mongoDB

When I Register [ input Email / password to DB ] success. Then, I want to login. If input[email/pass] == document in collection >> go to next page, else console.log['wrong email/pass']

I try to wirte IF/else code but I don't know check condition.

This code is Register form

app.post('/register',function(req,res){
    MongoClient.connect(url, function(err, db) {
  if (err) throw err;

  let dbo = db.db("project");
  let myobj = { Email: req.body.email, Password: req.body.psw } ;

  dbo.collection("Register").insertOne(myobj, function(err, res) {
    if (err) throw err;
    console.log(" document inserted");
    db.close();
  });
});

});

This code is Login form

app.post('/index',function(req,res){

MongoClient.connect(url, function(err, db) {
  if (err) throw err;

  let dbo = db.db("project");

  let cursor = dbo.collection('Register').find();
   cursor.each(function(err,doc) {
    if (doc == req.body.email && req.body.psw){
      console.log("goto next page");

    }
    else{
      console.log('wrong');
    }

   });
    db.close();
  });
});

Correct input and wrong input Output is = Wrong

Pls insert loop check all of array pls.

app.post('/index',function(req,res){

MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  var dbo = db.db("project");

  dbo.collection("Register").findOne({}, function(err, result) {

    if (result.Email == req.body.email && result.Password == req.body.psw) {
      console.log("OK");
    }
    else{
      console.log(result.Email && result.Password);
    }
    db.close();
  });
});
});


Solution 1:[1]

You have to compare individual values, like so:

if (doc.Email == req.body.email &&  doc.Password == req.body.psw){
  console.log("goto next page");

}

Solution 2:[2]

Firstly you should check for valid request body and the you should do a fineone query instead of running a for-loop and checking. see the corrected one below :

app.post("/index", function(req, res) {
  let {
    email,
    psw
  } = req.body;
  if (email && psw) {
    console.log("wrong credentials");
    return;
  } else {
    MongoClient.connect(url, function(err, db) {
      if (err) throw err;
      let dbo = db.db("project");
      let data = dbo.collection("Register").findOne({
        Email: email,
        Password: psw
      });
      if (data) {
        console.log("goto next page");
      } else {
        console.log("wrong");
      }
      db.close();
    });
  }
});

Solution 3:[3]

I'm late to the party but I just found a solution to a similar problem and wanted to share.

If you have input values in javascript and want to use them in a mongodb query you need to make them in to strings.

Assuming user._id is a value coming from a javascript function call.

This will work:

{ userId: { $eq: ${user._id} } } ?

This won't work:

{ userId: { $eq: user._id } } ?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Vikash_Singh
Solution 3 Emanuel Lindström