I am currently working on a project using the MEAN stack, and I have a task that requires executing a query in MongoDB. The query is as follows: db.locations.find({"name":/sometext/}); This query will return all results where it finds "sometext" in the name field.
Here is an example of my Express code:
app.post('/api/request/test1', function(req, res) {
res.contentType('application/json');
console.log(req.body.name);
var a = req.body.name;
var x = /+a+/;
locations.find({"name":x}).toArray(function (err, items){
res.send(items);
})
})
However, the code above is not functioning correctly as it gives an error with var x = /+a+/.
The desired end result is to have something like this: Let's say the variable a="TEST", so x should be /TEST/ without any single or double quotes, otherwise MongoDB won't recognize it.
Thank you.