"SQL 'like'语句在mongodb中的用法"

42 浏览
0 Comments

"SQL 'like'语句在mongodb中的用法"

这个问题在这里已经有了答案

如何使用“类似”查询MongoDB?

MongoDB/mongoose中是否有像SQL语言中的“like”语句?

使用它的唯一原因是实现全文搜索。

admin 更改状态以发布 2023年5月24日
0
0 Comments

我们可以使用Java API来实现在MongoDB中使用正则表达式的两种方法:

表达式1:我需要在文档字段中搜索以某个字符串开头的内容

String strpattern ="your regular expression";
Pattern p = Pattern.compile(strpattern);
BasicDBObject obj = new BasicDBObject() 
obj.put("key",p);

例如:(假设我想搜索所有以'a'开头的名字)

String strpattern ="^a";
Pattern p = Pattern.compile(strpattern);
BasicDBObject obj = new BasicDBObject() 
obj.put("name",p);

表达式2:我需要在一个字段中搜索多个单词,可以使用以下方法

String pattern = "\\b(one|how many)\\b";
BasicDBObject obj = new BasicDBObject();
//if you want to check case sensitive
obj.put("message", Pattern.compile(pattern,Pattern.CASE_INSENSITIVE));

0
0 Comments

MongoDB支持正则表达式。

0