I'm building a dynamic search query for a Mongo database.
In short, and not directly related to the question... it looks like this:
var searchCriteria = {}; <-- start with empty object
return db.users.find(searchCriteria,
{ sort: { username: 1 }
});
The values for searchCriteria come from a search form, basically like this:
var filter = $(form).find('select[name=filter]').val();
var query = $(form).find('[name=query]').val();
searchCriteria[filter] = query <-- Using a dynamic key
Example output from the form:
console.log(searchCriteria);
>> { username: "jdoe" }
So here's my hangup. I need to "unstringify" the query within the searchCriteria, and turn it into this:
>> { username: /jdoe/ }
I've tried replace, among other things, but it keeps ending up as a string value. I need the /query/ in that format for the Mongo find query.
Any suggestions? Thank you :)
new RegExp(string);{}is an empty object, not an empty array.var searchCriteria = {};is not an array, its an object