Checking if a field contains a string

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?


你可以用下面的代码来完成。

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

As Mongo shell support regex, that's completely possible.

db.users.findOne({"username" : /.*son.*/});

If we want the query to be case-insensitive, we can use "i" option, like shown below:

db.users.findOne({"username" : /.*son.*/i});

See: http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-RegularExpressions


http://www.mongodb.org/display/DOCS/SQL+to+Mongo+Mapping+Chart

http://php.net/manual/en/mongo.sqltomongo.php

MySQL

SELECT * FROM users WHERE username LIKE "%Son%"

MongoDB

db.users.find({username:/Son/})
链接地址: http://www.djcxy.com/p/86398.html

上一篇: 在两个日期MongoDB之间查找对象

下一篇: 检查一个字段是否包含字符串