$regex to find a document in mongodb that contains a string -
i working on db query in mongo
need query document regular expressing string
field(fieldtoquery).
the datastructure like
{ fieldtoquery : "4052577300", someotherfield : "some value", ... ... }
i have value "804052577300"
, using have query above document.
how achieve same using $regex operator?
update:
i need ends regex in mongo
.
you sort of reverse regex query create regex using fieldtoquery
value , test against query value:
var value = "804052577300"; db.test.find({ $where: "new regexp(this.fieldtoquery + '$').test('" + value + "');" });
the $where
operator allows execute arbitrary javascript against each doc in collection; this
current doc, , truthy result of expression determines whether doc should included in result set.
so in case, new regexp
built each doc's fieldtoquery
field $
@ end anchor search end of string. test
method of regex called test whether value
string matches regex.
the $where
string evaluated server-side, value
's value must evaluated client-side directly including value $where
string.
note $where
queries can quite slow can't use indexes.
Comments
Post a Comment