regex - Mongodb distinct query with contains query -
i have collection name = user
{ id : 1, name : "gaurav", skills : "c++ html css" }
when searching users have c++ skill in next query getting results
db.user.find({skills:{contains:"c++"}});
but when searching unique names user using same status m not getting result
db.user.distinct('name',{skills:{contains:"c++"}});
can help me doing wrong
the "contains" not valid keyword mongodb queries. need $regex
submits general "regular expression" statement matching pcre specifications:
db.user.distinct( "name", { "skills": { "$regex": "c\+\+" } })
if using javascript language safe:
class="lang-js prettyprint-override">db.user.distinct( "name", { "skills": /c\+\+/ })
to determine if string "c++" occurred somewhere within string value of field beingness tested. +
character reserved in "regex" operations , hence need escape \
char standard escaping mechanism.
on info result:
class="lang-js prettyprint-override">db.user.distinct( "name", { "skills": { "$regex": "c\+\+" } }) [ "gaurav" ]
regex mongodb mongodb-query
No comments:
Post a Comment