I have a database and collection set up which has a BSONRegExp field named “myregex”. I have a number of entries each with different regex strings.
When I run the following against the database via shell I get a correct response back:
db.testcol.find(function() { return this.myregex.test(‘a’); } )
As you can see I am using the JavaScript test() method to check whether or not there is a match returned. I have attempted to migrate this over to NodeJS but I am unable to find any documentation as to how this query should be formed. I have been using the MongoDB Node.js Driver MongoDB Node.js Driver The following is the cloest I am able to get however it returns the entire collection and not just those matching the regex match to the inputted string (the string, in this case, being “a”).
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db("Testingdb");
dbo.collection("regextest").find( { $where: function() { return this.myregex.test('a') } } ).toArray(function (err,result) {
if (err) {
console.log(err);
} else {
console.log(result);
}
});
});
Any assistance or suggestions would be appreciated.