我想用SQL查询一些东西,比如查询:
SELECT * FROM users WHERE name LIKE '%m%'
如何在MongoDB中实现这一点?我在文档中找不到这样的操作员。
我想用SQL查询一些东西,比如查询:
SELECT * FROM users WHERE name LIKE '%m%'
如何在MongoDB中实现这一点?我在文档中找不到这样的操作员。
当前回答
Use:
db.customer.find({"customerid": {"$regex": "CU_00000*", "$options": "i"}}).pretty()
当我们搜索字符串模式时,最好使用上面的模式,因为我们不确定大小写。
其他回答
如果您想在MongoDB中进行“like”搜索,那么应该使用$regex。通过使用它,查询将是:
db.product.find({name:{$regex:/m/i}})
有关更多信息,您也可以阅读文档-$regex
MongoRegex已被弃用。
使用MongoDB \ BSON \ Regex:
$regex = new MongoDB\BSON\Regex ( '^m');
$cursor = $collection->find(array('users' => $regex));
//iterate through the cursor
以下是使用“从”范例的命令:
db.customer.find({"customer_name" : { $regex : /^startswith/ }})
您有两个选择:
db.users.find({"name": /string/})
or
db.users.find({"name": {"$regex": "string", "$options": "i"}})
对于第二个,您有更多的选项,如选项中的“i”,以使用不区分大小写的方式查找。
关于“字符串”,可以使用“.string.”(%string%),或“string.*”(string%)和“.*string”(%sstring)。您可以根据需要使用正则表达式。
还可以按如下方式使用通配符过滤器:
{"query": { "wildcard": {"lookup_field":"search_string*"}}}
请务必使用*。