Probably your best litmus test for this is using a JavaScript REPL, much like the mongo shell with your basic principle which is:
var string = "star wars episode VII";
/string/.test("star wars episode VII - a new disney");
Which of course is just going to return false
.
The general problem here is that you cannot use a variable within the /../
syntax of a regular expression and to do so you would actually need to define this as a RegExp
object like so:
var patt = new RegExp("star wars episode VII");
patt.test("star wars episode VII - a new disney");
So the two statements are not actually equal, and therefore one returns true
while the other form returns false.
It is kind of true ( though equally horrible to the JavaScript execution ) that you do not need this at all, and could simply do a Regex search:
db.tag.find({ "name": /star wars episode VII/ })
MongoDB supports that, but it really isn't advised, but either is usage of the $where
operator. See $regex
for more information.