有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

两个或多个运算符列表上的java逻辑AND运算符

我使用下面的查询searchQuery来应用正则表达式

BasicDBObject searchQuery = new BasicDBObject();
Pattern regex;
BasicDBList or = new BasicDBList();
for (String s : statistics) {
    regex = Pattern.compile(s);
    DBObject statRegex = new BasicDBObject("_id", regex);
    or.add(statRegex);
}
searchQuery.put("$or", or);

它在_id字段中查找名为statistics的数组列表中字符串之一的值的结果。(例如“错误”、“超时”、“响应时间”…)

如何通过两个or列表查询数据库。例如,我想匹配_id字段等于statistics中包含的字符串,而name字段等于networks中包含的字符串

通过阅读其他主题,我认为解决方案可能是使用$elemMatch,尽管我对高级查询在mongoDB中的工作方式了解有限


共 (1) 个答案

  1. # 1 楼答案

    您可以使用$和$或组合使用

    test:Mongo > db.test.insert({ _id : "abhi", name : "kumar" })
    test:Mongo > db.test.find({ $and : [ {$or : [ { _id : /abhi/ } ]}, {$or : [ { name : /kumar/ } ] } ] })
    { "_id" : "abhi", "name" : "kumar" }