I would like to make a MongoDB query for documents based on the regex expression that I am constructing. For example, for example, I constructed a simple regular expression as follows: this is a combination of a random letter and a random number for Nodejs
var randnum = Math.floor((Math.random() * 10) + 1); var alpha = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','X','Y','Z']; var randletter = alpha[Math.floor(Math.random() * alpha.length)]; var val = randletter + randnum + '.*;
I tried various combinations for regex variable, for example
var stream = collection.find({"FirstName": /val/).stream(); && var stream = collection.find({"FirstName": /$val/).stream(); && var stream = collection.find({"FirstName": {$in : [{$regex:val}]}}).stream() && var stream = collection.find({"FirstName": {$in : [{$regex:$val}]}}).stream()
No, it seems to work. However, when I write the actual regular expression, I get entries, for example.
var stream = collection.find({"FirstName": /J.*/).stream();
Any help would be appreciated.
Thanks ganesh