简体   繁体   English

如何使用 Java 在 MongoDB 中同时使用 AND 和 OR 子句执行查询?

[英]How to execute queries with both AND and OR clauses in MongoDB with Java?

I want to execute query in MongoDB 3.2 with Java Driver 3.2, which contains both $and and $or clauses at the same time.我想使用 Java Driver 3.2 在 MongoDB 3.2 中执行查询,它同时包含$and$or子句。

With the reference , I tried the following approach:通过参考,我尝试了以下方法:

List<Document> criteria1 = new ArrayList<>();
List<Document> criteria2 = new ArrayList<>();

criteria1.add(new Document("fetchStatus", new Document("$gte", FetchStatus.PROCESSED_NLP.getID())));
criteria1.add(new Document("fetchStatus", new Document("$lte", fetchStatusParam)));
criteria1.add(new Document("episodeID", new Document("$in", episodeIDs)));

criteria2.add(new Document("fetchStatus", new Document("$eq", PROCESSED_FETCH.getID())));
criteria2.add(new Document("isFullTextRet", new Document("$eq", false)));

BasicDBList or = new BasicDBList();
or.add(criteria1);
or.add(criteria2);

DBObject query = new BasicDBObject("$or", or);
ArrayList<Document> results = dbC_Coll.find(query).into(new ArrayList<>());

Where the criteria1 and criteria2 should be connected with $or while within criteria1 clause the $and should be applied.如果criteria1criteria2应该与$or连接,而在criteria1子句中$and应该应用$and

The problem is that in MongoDB Java Driver 3.2 there is such no method and I get the Cannot resolve method find(com.mongodb.DBObject) error.问题是在 MongoDB Java Driver 3.2 中没有这样的方法,我得到了Cannot resolve method find(com.mongodb.DBObject)错误。

My question:我的问题:
How can I compose a query such as (A && B) || (X && Y)如何编写查询,例如(A && B) || (X && Y) (A && B) || (X && Y) in MongoDB Java Driver 3.2? (A && B) || (X && Y)在 MongoDB Java Driver 3.2 中?

Personally, I find it far less confusing to construct the object sequences just like U would with a JSON structure to enhance readability.就我个人而言,我发现像 U 一样使用 JSON 结构构建对象序列以增强可读性,并没有那么令人困惑。 But it's still just Document() wherever you see {} and List wherever you see [] :但它仍然只是Document()无论你在哪里看到{}List无论你在哪里看到[]

Document query = new Document(
    "$or", Arrays.asList(
        // First document in $or
        new Document(
            "fetchStatus", 
            new Document( "$gte", FetchStatus.PROCESSED_NLP.getID() )
            .append("$lte", fetchStatusParam)
        )
        .append("episodeID", new Document( "$in", episodeIDs)),
        // Second document in $or
        new Document("fetchStatus", PROCESSED_FETCH.getID())
        .append("isFullTextRet", false)
    )
);

Which is basically the same as:这与以下内容基本相同:

   {
       "$or": [
           {
               "fetchStatus": { 
                   "$gte": FetchStatus.PROCESS_NLP.getID(),
                   "$lte": fetchStatusParam
               },
               "episodeID": { "$in": episodeIDs }
           },
           {
               "fetchStatus": PROCESSED_FETCH.getID(),
               "isFullTextRet": false
           }
       ]
   }

Also there is no need for "explicit" $eq operators, since "equals" is actually the default meaning of a value assignment in a query property anyway.也不需要“显式” $eq运算符,因为“等于”实际上是查询属性中值分配的默认含义。

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

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