Mongoose query: how to pass a variable
You could do:
Xcom.find({ $where : "this.NIC_No == '" + variableThatContainsNumber + "'" }, function ( err, xcoms ){ ...});
Or even better:
Xcom.find({NIC_No: variableThatContainsNumber}, function(err, doc) { ...});
The second one is much better because it does not require JavaScript execution within MongoDB.
This will work:
{ $where : "this.NIC_No == '" + n + "'" }
Although you're passing in a value that the user could directly set, which probably isn't a great idea. If n
is always a number, be safer to make sure using something like:
var n = parseInt(req.body.NIC_No, 10);
And if n
is always a number, you don't need to put it in quotes in the query either:
{ $where : "this.NIC_No == " + n }