如何使用“like”查询MongoDB
我想使用 SQL 的 like
查询来查询某些内容:
SELECT * FROM users WHERE name LIKE '%m%'
如何在 MongoDB 中实现相同的目的?我在文档like的运算符一个>。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
您有两种选择:
或
对于第二个选择,您有更多选项,例如选项中的“i”来查找不区分大小写的情况。
关于“字符串”,您可以使用“.string.”(%string%)或“string.*”(string%)和“.*string)(%string)”您可以根据需要使用正则表达式。
You have two choices:
or
For the second one, you have more options, like "i" in options to find using case insensitive.
And about the "string", you can use like ".string." (%string%), or "string.*" (string%) and ".*string) (%string) for example. You can use a regular expression as you want.
如果使用 Node.js,它会显示 你可以这样写:
另外,你可以这样写:
If using Node.js, it says that you can write this:
Also, you can write this:
您已经得到了答案,但要与不区分大小写的正则表达式匹配,您可以使用以下查询:
/m/i
中的i
表示不区分大小写,.pretty()
提供更漂亮的输出。Already you got the answers, but to match with a regular expression with case insensitivity, you could use the following query:
The
i
in the/m/i
indicates case insensitivity and.pretty()
provides a prettier output.对于 Node.js 中的 Mongoose:
For Mongoose in Node.js:
在 MongoDb 中,可以像使用 MongoDb 引用运算符正则表达式(regex)。
对于同一个前任。
In MongoDb, can use like using MongoDb reference operator regular expression(regex).
For Same Ex.
使用 MongoDB Compass,您需要使用严格模式语法,如下所示:(
在 MongoDB Compass 中,使用
"
而不是'
很重要)With MongoDB Compass, you need to use the strict mode syntax, as such:
(In MongoDB Compass, it's important that you use
"
instead of'
)您可以使用MongoDB 2.6的新功能:
You can use the new feature of MongoDB 2.6:
在 Node.js 项目中并使用 Mongoose ,使用like查询:
In a Node.js project and using Mongoose, use a like query:
您可以使用 where 语句构建任何 JavaScript 脚本:
参考:$where
You can use a where statement to build any JavaScript script:
Reference: $where
字符串yourdb = {deepakparmar,dipak,parmar}
ans deepakparmar
ans deepakparmar,dipak
ans deepakparmar,parmar
String yourdb={deepakparmar, dipak, parmar}
ans deepakparmar
ans deepakparmar, dipak
ans deepakparmar, parmar
在 Go 和 mgo 驱动程序中:
结果是所需类型的结构体实例。
In Go and the mgo driver:
where the result is the struct instance of the sought-after type.
在 SQL 中,“like”查询如下所示:
在 MongoDB 控制台中,它如下所示:
另外,
pretty()
方法将生成格式化的 JSON 结构在所有更具可读性的地方。In SQL, the ‘like’ query looks like this:
In the MongoDB console, it looks like this:
In addition, the
pretty()
method will produce a formatted JSON structure in all the places which is more readable.对于 PHP mongo就像。
我在使用 PHP mongo 时遇到了一些问题,比如。我发现连接正则表达式参数在某些情况下会有所帮助 - PHP mongo find field以开头。
例如,
For PHP mongo Like.
I had several issues with PHP mongo like. I found that concatenating the regular expression parameters helps in some situations - PHP mongo find field starts with.
For example,
将模板文字与变量一起使用也可以:
{"firstname": {$regex : `^${req.body.firstname}.*` , $options: 'si' }}
Using template literals with variables also works:
{"firstname": {$regex : `^${req.body.firstname}.*` , $options: 'si' }}
正则表达式的处理成本很高。
另一种方法是创建文本索引,然后使用
$search
进行搜索。创建要搜索的字段的文本索引:
在文本索引中搜索字符串:
Regular expressions are expensive to process.
Another way is to create an index of text and then search it using
$search
.Create a text index of fields you want to make searchable:
Search for a string in the text index:
使用正则表达式匹配如下。 “i”表示不区分大小写。
Use regular expressions matching as below. The 'i' shows case insensitivity.
似乎同时使用 JavaScript
/regex_pattern/
模式和 MongoDB{'$regex': 'regex_pattern'}
模式都是有原因的。请参阅: MongoDB RegEx 语法限制这不是一个完整的正则表达式教程,但在看到上面的高票模棱两可的帖子后,我受到了运行这些测试的启发。
It seems that there are reasons for using both the JavaScript
/regex_pattern/
pattern as well as the MongoDB{'$regex': 'regex_pattern'}
pattern. See: MongoDB RegEx Syntax RestrictionsThis is not a complete regular expression tutorial, but I was inspired to run these tests after seeing a highly voted ambiguous post above.
like 查询如下所示:
对于 Scala ReactiveMongo API,
A like query would be as shown below:
For the Scala ReactiveMongo API,
如果你使用Spring-Data MongoDB,你可以这样做:
If you are using Spring-Data MongoDB, you can do it in this way:
如果您有一个字符串变量,则必须将其转换为正则表达式,因此 MongoDB 将对其使用 like 语句。
与以下结果相同:
If you have a string variable, you must convert it to a regex, so MongoDB will use a like statement on it.
Is the same result as:
查找结果的一种方法与类似查询相同:
其中
i
用于不区分大小写的获取数据。另一种方法是我们还可以得到结果:
上面将提供包含 aus 的名称中包含 aus 的结果。
One way to find the result as with equivalent to a like query:
Where
i
is used for a case-insensitive fetch data.Another way by which we can also get the result:
The above will provide the result which has the aus in the name containing aus.
使用聚合子字符串搜索(带索引!!!):
Use aggregation substring search (with index!!!):
您可以使用正则表达式进行查询:
如果字符串来自用户,也许您想在使用字符串之前对其进行转义。这将防止用户的文字字符被解释为正则表达式标记。
例如,搜索字符串“A”。如果不转义,也将匹配“AB”。
您可以在使用字符串之前使用简单的
replace
对其进行转义。我将其设置为可重用的函数:所以现在,字符串变成了不区分大小写的模式,也与文字点匹配。示例:
现在我们准备好随时生成正则表达式:
You can query with a regular expression:
If the string is coming from the user, maybe you want to escape the string before using it. This will prevent literal chars from the user to be interpreted as regex tokens.
For example, searching the string "A." will also match "AB" if not escaped.
You can use a simple
replace
to escape your string before using it. I made it a function for reusing:So now, the string becomes a case-insensitive pattern matching also the literal dot. Example:
Now we are ready to generate the regular expression on the go:
如果您想在 MongoDB 中进行“类似”搜索,那么您应该使用 $regex。通过使用它,查询将是:
有关更多信息,您也可以阅读文档 - $正则表达式
If you want a 'like' search in MongoDB then you should go with $regex. By using it, the query will be:
For more, you can read the documentation as well - $regex
那必须是:
或者,类似:
您正在寻找某个地方包含“m”的内容(SQL 的 '
%
' 运算符相当于正则表达式' '.*
'),而不是将“m”锚定到字符串开头的内容。注意: MongoDB 使用正则表达式(参见文档) 比 SQL 中的“LIKE”更强大。使用正则表达式,您可以创建您想象的任何模式。
有关正则表达式的更多信息,请参阅正则表达式< /a>(MDN)。
That would have to be:
Or, similar:
You're looking for something that contains "m" somewhere (SQL's '
%
' operator is equivalent to regular expressions' '.*
'), not something that has "m" anchored to the beginning of the string.Note: MongoDB uses regular expressions (see docs) which are more powerful than "LIKE" in SQL. With regular expressions you can create any pattern that you imagine.
For more information on regular expressions, refer to Regular expressions (MDN).
因此:
对于:
输出:patrick, petra
对于:
输出:patrick
对于:
输出:pedro
Therefore:
For:
Output: patrick, petra
For:
Output: patrick
For:
Output: pedro
在
你可以这样做:
In
you can do:
以下是使用正则表达式进行字符串搜索的不同类型的要求和解决方案。
您可以使用包含单词的正则表达式,即like。您也可以使用
$options =>; i
用于不区分大小写的搜索。包含
string
不包含
string
,仅包含正则表达式不区分大小写
string
以string
开头 以
string
结尾
string
保留正则表达式备忘单 作为书签,以及您可能需要的任何其他更改的参考。
, '$options' : 'i'}})保留正则表达式备忘单 作为书签,以及您可能需要的任何其他更改的参考。
, '$options' : 'i'}})不区分大小写
string
以string
开头 以
string
结尾
string
保留正则表达式备忘单 作为书签,以及您可能需要的任何其他更改的参考。
, '$options' : 'i'}})开头 以
string
结尾
string
保留正则表达式备忘单 作为书签,以及您可能需要的任何其他更改的参考。
, '$options' : 'i'}})不区分大小写
string
以string
开头 以
string
结尾
string
保留正则表达式备忘单 作为书签,以及您可能需要的任何其他更改的参考。
Here are different types of requirements and solutions for string search with regular expressions.
You can do with a regular expression which contains a word, i.e., like. Also you can use
$options => i
for a case insensitive search.Contains
string
Doesn't contain
string
, only with a regular expressionExact case insensitive
string
Start with
string
End with
string
Keep Regular Expressions Cheat Sheet as a bookmark, and a reference for any other alterations you may need.
, '$options' : 'i'}})Keep Regular Expressions Cheat Sheet as a bookmark, and a reference for any other alterations you may need.
, '$options' : 'i'}})Exact case insensitive
string
Start with
string
End with
string
Keep Regular Expressions Cheat Sheet as a bookmark, and a reference for any other alterations you may need.
, '$options' : 'i'}})Start with
string
End with
string
Keep Regular Expressions Cheat Sheet as a bookmark, and a reference for any other alterations you may need.
, '$options' : 'i'}})Exact case insensitive
string
Start with
string
End with
string
Keep Regular Expressions Cheat Sheet as a bookmark, and a reference for any other alterations you may need.
在 PHP 中,您可以使用以下代码:
In PHP, you could use the following code:
您可以在 MongoDB 中使用正则表达式。
例如,
You would use a regular expression for that in MongoDB.
For example,