如何使用java api用“like”查询mongodb?

时间:2021-11-28 11:29:03

this question is very similar to another post

这个问题与另一篇文章非常相似

I basically want to use the mongodb version of the sql "like" '%m%' operator

我基本上想要使用mongodb版本的sql“like”'%m%'运算符

but in my situation i'm using the java api for mongodb, while the other post is using mongodb shell

但在我的情况下,我正在使用java api for mongodb,而另一篇文章是使用mongodb shell

i tried what was posted in the other thread and it worked fine

我尝试了在另一个帖子中发布的内容,它运行正常

db.users.find({"name": /m/})

but in java, i'm using the put method on the BasicDBObject and passing it into the find() method on a DBCollections object

但在java中,我在BasicDBObject上使用put方法并将其传递给DBCollections对象上的find()方法

BasicDBObject q = new BasicDBOBject();
q.put("name", "/"+m+"/");
dbc.find(q);

but this doesn't seem to be working.

但这似乎不起作用。

anyone has any ideas?

有人有什么想法?

6 个解决方案

#1


64  

You need to pass an instance of a Java RegEx (java.util.regex.Pattern):

您需要传递Java RegEx的实例(java.util.regex.Pattern):

BasicDBObject q = new BasicDBObject();
q.put("name",  java.util.regex.Pattern.compile(m));
dbc.find(q);

This will be converted to a MongoDB regex when sent to the server, as well as any RegEx flags.

这将在发送到服务器时转换为MongoDB正则表达式,以及任何RegEx标志。

#2


3  

You must first quote your text and then use the compile to get a regex expression:

您必须首先引用您的文本,然后使用编译来获取正则表达式:

q.put("name",  Pattern.compile(Pattern.quote(m)));

Without using java.util.Pattern.quote() some characters are not escaped.

不使用java.util.Pattern.quote(),一些字符不会被转义。

e.g. using ? as the m parameter will throw an exception.

例如使用?因为m参数会抛出异常。

#3


3  

To make it case insensitive:

使其不区分大小写:

Document doc = new Document("name", Pattern.compile(keyword, Pattern.CASE_INSENSITIVE));
collection.find(doc);

#4


1  

In spring data mongodb, this can be done as:

在spring数据mongodb中,这可以这样做:

Query query = new Query();
query.limit(10);        
query.addCriteria(Criteria.where("tagName").regex(tagName));
mongoOperation.find(query, Tags.class);

#5


0  

Document doc = new Document("name", Pattern.compile(keyword));
collection.find(doc);

#6


0  

Might not be the actual answer, ( Executing the terminal query directly )

可能不是真正的答案,(直接执行终端查询)

public void displayDetails() {
    try {
        // DB db = roleDao.returnDB();
        MongoClient mongoClient = new MongoClient("localhost", 5000);
        DB db = mongoClient.getDB("test");
        db.eval("db.test.update({'id':{'$not':{'$in':[/su/]}}},{$set:{'test':'test3'}},true,true)", new Object[] {});
        System.out.println("inserted  ");

    } catch (Exception e) {
        System.out.println(e);
    }
}

#1


64  

You need to pass an instance of a Java RegEx (java.util.regex.Pattern):

您需要传递Java RegEx的实例(java.util.regex.Pattern):

BasicDBObject q = new BasicDBObject();
q.put("name",  java.util.regex.Pattern.compile(m));
dbc.find(q);

This will be converted to a MongoDB regex when sent to the server, as well as any RegEx flags.

这将在发送到服务器时转换为MongoDB正则表达式,以及任何RegEx标志。

#2


3  

You must first quote your text and then use the compile to get a regex expression:

您必须首先引用您的文本,然后使用编译来获取正则表达式:

q.put("name",  Pattern.compile(Pattern.quote(m)));

Without using java.util.Pattern.quote() some characters are not escaped.

不使用java.util.Pattern.quote(),一些字符不会被转义。

e.g. using ? as the m parameter will throw an exception.

例如使用?因为m参数会抛出异常。

#3


3  

To make it case insensitive:

使其不区分大小写:

Document doc = new Document("name", Pattern.compile(keyword, Pattern.CASE_INSENSITIVE));
collection.find(doc);

#4


1  

In spring data mongodb, this can be done as:

在spring数据mongodb中,这可以这样做:

Query query = new Query();
query.limit(10);        
query.addCriteria(Criteria.where("tagName").regex(tagName));
mongoOperation.find(query, Tags.class);

#5


0  

Document doc = new Document("name", Pattern.compile(keyword));
collection.find(doc);

#6


0  

Might not be the actual answer, ( Executing the terminal query directly )

可能不是真正的答案,(直接执行终端查询)

public void displayDetails() {
    try {
        // DB db = roleDao.returnDB();
        MongoClient mongoClient = new MongoClient("localhost", 5000);
        DB db = mongoClient.getDB("test");
        db.eval("db.test.update({'id':{'$not':{'$in':[/su/]}}},{$set:{'test':'test3'}},true,true)", new Object[] {});
        System.out.println("inserted  ");

    } catch (Exception e) {
        System.out.println(e);
    }
}