node.js - Mongoose query: how to pass a variable -


how pass 'n' instead of hard coded '5' in xcom.find({ $where : "this.nic_no == '5'" } below?

exports.query_by_nic = function ( req, res ){   var n = req.body.nic_no;   console.log(n); //n prints correctly expected   xcom.find({ $where : "this.nic_no == '5'" }, function ( err, xcoms ){ //works '5' how query 'n'?      res.render( 'query_by_nic', {         title   : 'query nic',         xcoms   : xcoms     });     console.log(err);     console.log(xcoms);   }); }; 

you do:

xcom.find({ $where : "this.nic_no == '" + variablethatcontainsnumber + "'" }, function ( err, xcoms ){     ... }); 

or better:

xcom.find({nic_no: variablethatcontainsnumber}, function(err, doc) {     ... }); 

the second 1 better because not require javascript execution within mongodb.


Comments