I'm trying to pass arguments into a mongo query - if I type the literal, it works fine, however, when I try to replace the literal with the variable (argument), it fails
imagine a data set with "things"
{"thing": {"name":"book","label":"Bobs"}}
{"thing": {"name":"blanket","label":"Bobs"},
{"thing": {"name":"books","label":"Jills"},
{"thing": {"name":"blankets","label":"Jills"},
I have a method to find "Book" "book" "Books" "books" or "blankets"
If I do this with the literal 'Book' - it works
function( name, callback ) {
Catalog
.find({ 'thing.name': {"$regex": /Book/, "$options": "i" }})
.exec(callback);
}
However - I want the argument 'name' to be used.
function( name, callback ) {
Catalog
.find({ 'thing.name': {"$regex": /name/, "$options": "i" } })
.exec(callback);
}
But this doesn't work, it appears to be looking for the literal 'name' not the passed in value. how to i escape this?