Mongodb – Checking if a field contains a string

mongodb

I'm looking for an operator, which allows me to check, if the value of a field contains a certain string.

Something like:

db.users.findOne({$contains:{"username":"son"}})

Is that possible?

Best Answer

You can do it with the following code.

db.users.findOne({"username" : {$regex : "son"}});