简体   繁体   English

如何在不使用模式匹配的情况下使用Java API使用“喜欢”查询mongodb?

[英]How to query mongodb with “like” using the java api without using Pattern Matching?

Currently I am using java to connect to MONGODB, I want to write this sql query in mongodb using java driver: 目前,我正在使用Java连接到MONGODB,我想使用Java驱动程序在mongodb中编写此sql查询:

select * from tableA where name like("%ab%")

is their any solution to perform the same task through java, the query in mongodb is very simple i know, the query is 是他们通过java执行相同任务的任何解决方案,mongodb中的查询非常简单,我知道,查询是

db.collection.find({name:/ab/})

but how to perform same task in java 但是如何在Java中执行相同的任务

Current I am using pattern matching to perform the task and code is 当前我正在使用模式匹配来执行任务,代码是

DBObject A = QueryBuilder.start("name").is(Pattern.compile("ab", 
                                           Pattern.CASE_INSENSITIVE)).get();

but it makes query very slow I think , does a solution exist that does not use pattern matching? 但是我认为这会使查询非常慢,是否存在不使用模式匹配的解决方案?

Can use Regular Expressions. 可以使用正则表达式。 Take a look at the following: 看一下以下内容:

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

Make sure you understand the potential performance impacts! 确保您了解潜在的性能影响!

Why do you fear the regular expressions? 您为什么害怕正则表达式? Once the expression is compiled they are very fast, and if the expression is "ab" the result is similar to a function that search a substring in a string. 编译表达式后,它们将非常快,如果表达式为“ ab”,则结果类似于在字符串中搜索子字符串的函数。

However to do what you need you have 2 possibilities: 但是,执行所需的操作有两种可能性:

  1. The first one, using regular expression, as you mention in your question. 正如您在问题中提到的,第一个使用正则表达式。 And I believe this is the best solution . 而且我相信这是最好的解决方案
  2. The second one, using the $where queries . 第二个,使用$where 查询

With $where queries you can specify expression like these 使用$where查询,您可以指定如下表达式

db.foo.find({"$where" : "this.x + this.y == 10"})
db.foo.find({"$where" : "function() { return this.x + this.y == 10; }"})

and so you can use the JavaScript .indexOf() on string fields. 因此您可以在字符串字段上使用JavaScript .indexOf()

Code snippet using the $regex clause (as mentioned by mikeycgto ) 使用$regex子句的代码 (如mikeycgto所述

String searchString = "ab";
DBCollection coll = db.getCollection("yourCollection");
query.put("name", 
  new BasicDBObject("$regex", String.format(".*((?i)%s).*", searchString)) );
DBCursor cur = coll.find(query);
while (cur.hasNext()) {
  DBObject dbObj = cur.next();
  // your code to read the DBObject ..       
}

As long as you are not opening and closing the connection per method call, the query should be fast. 只要不按每个方法调用打开和关闭连接,查询就应该很快。

DBObject A = QueryBuilder.start("name").is(Pattern.compile("ab", 
                                       Pattern.CASE_INSENSITIVE)).get();

I think this is one of the possible solution, you need to create index to achieve those. 我认为这是可能的解决方案之一,您需要创建索引来实现这些目标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM