When passing an object to the MongoClient collection.find() method, I'm facing an issue where I don't want quotes around the values. If I pass a literal, it works fine:
cursor = collection.find({EmployeeName: {'$regex': /Jo/i}});
However, if I try to construct the object like this:
var queryt = "/Jo/i";
var queryo = {
EmployeeName: {'$regex': queryt}
};
It doesn't work. When I check console.log(queryo)
, I notice that there are quotes present around "Jo/i":
{ EmployeeName: { '$regex': '/Jo/i' } }
In my application, 'queryt' is derived by extracting values from the 'query' object returned from an 'get' function in Express. For example, I make a call with an URL ending as "?EmployeeName:/Jo/i". In my "get" function, I do:
var queryt=req.query.EmployeeName;
Essentially, I am using this node.js app as a backend server that accepts queries via HTTP get requests.
I have tried different methods to remove the quotes but without success.
Apologies if this seems like a beginner question, I am new to this and have spent hours trying to resolve it. Hopefully, a more experienced node.js developer can guide me on how to structure this object so that collection.find will accept it. Thank you!