Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何编写(a | | B | | C)&&;MongoDB Java驱动程序3.2中的(X | | Y)_Java_Mongodb - Fatal编程技术网

如何编写(a | | B | | C)&&;MongoDB Java驱动程序3.2中的(X | | Y)

如何编写(a | | B | | C)&&;MongoDB Java驱动程序3.2中的(X | | Y),java,mongodb,Java,Mongodb,不幸的是,我找不到Mongo3.2Java驱动程序的示例,例如“(A或B或C)和(D或E或F或G)” 括号内的参数数量将是可变的-最多为100个 有趣的是,我找到了“(A&&B)|(X&&Y)”的例子,但它对我没有帮助 我的代码产生错误: MongoQueryException:查询失败,错误代码为2,错误消息“$or/$和/$nor条目必须是完整对象” List<Document> docs = new ArrayList<>(); for (Integer ln:

不幸的是,我找不到Mongo3.2Java驱动程序的示例,例如“(A或B或C)和(D或E或F或G)”

括号内的参数数量将是可变的-最多为100个

有趣的是,我找到了“(A&&B)|(X&&Y)”的例子,但它对我没有帮助

我的代码产生错误:

MongoQueryException:查询失败,错误代码为2,错误消息“$or/$和/$nor条目必须是完整对象”

List<Document> docs = new ArrayList<>();

for (Integer ln: input.getLastnames()) {
        docs.add(new Document("lastname",ln));
    }

    Document queryLN = new Document(
            "$or", Arrays.asList(docs)
    );

    docs.clear();
    for (Integer fn: input.getFirstnames()) {
        docs.add(new Document("firstname",fn));
    }

    Document queryFN = new Document(
            "$or", Arrays.asList(docs)
    );

    Document query = new Document(
            "$and", Arrays.asList(queryFN,queryLN));

    List<Document> result = collectionMain.find(query).into(new ArrayList<Document>());
List docs=new ArrayList();
for(整数ln:input.getLastnames()){
添加文件(新文件(“姓氏”,ln));
}
文档queryLN=新文档(
“$or”,数组.asList(文档)
);
docs.clear();
for(整数fn:input.getFirstnames()){
添加文件(新文件(“名字”,fn));
}
文档查询n=新文档(
“$or”,数组.asList(文档)
);
文档查询=新文档(
“$and”,Arrays.asList(queryFN,queryLN));
List result=collectionMain.find(query).into(new ArrayList());
当您有一个很长的或未知的条件列表时,您应该在这种条件下使用“in”查询

示例代码:

try {
        MongoClient mongo = new MongoClient();
        DB db = mongo.getDB("so");
        DBCollection coll = db.getCollection("employees");

        List<Integer> ageList = new ArrayList<>();
        ageList.add(30);
        ageList.add(35);

        List<String> nameList = new ArrayList<>();
        nameList.add("Anna");

        BasicDBObject query = new BasicDBObject("$and", Arrays.asList(
            new BasicDBObject("age", new BasicDBObject("$in", ageList)),
            new BasicDBObject("name", new BasicDBObject("$in", nameList)))
        );

        DBCursor cursor = coll.find(query);
        while(cursor.hasNext()) {
            System.out.println(cursor.next());
        }
}catch (Exception ex){
        ex.printStackTrace();
}
上述代码生成此查询:

db.employees.find({"$and":[{"age":{"$in":[30, 35]}},{"name":{"$in":["Anna"]}}]});
输出为:

{ "_id" : { "$oid" : "57ff3e5e3dedf0228d4862ad"} , "name" : "Anna" , "dept" : "Admin" , "languages" : [ "english" , "hindi"] , "age" : 35.0 , "totalExp" : 11.0}
关于这个主题的一篇好文章:


同时阅读以下内容:让我们首先了解您的代码。我们将用简单语句替换for循环,并添加一些print语句,从而使其变得简单

List<Document> docs = new ArrayList<>();

docs.add(new Document("lastname","Walker"));
docs.add(new Document("lastname","Harris"));    

Document queryLN = new Document("$or", Arrays.asList(docs));

docs.clear();

System.out.println(queryLN.toJson());//{ "$or" : [[]] } 

docs.add(new Document("firstname", "Pat"));
docs.add(new Document("firstname", "Matt"));

Document queryFN = new Document("$or", Arrays.asList(docs));

System.out.println(queryLN.toJson());//{ "$or" : [[{ "firstname" : "Pat" }, { "firstname" : "Matt" }]] }
System.out.println(queryFN.toJson());//{ "$or" : [[{ "firstname" : "Pat" }, { "firstname" : "Matt" }]] }

Document query = new Document("$and", Arrays.asList(queryFN, queryLN));

System.out.println(query.toJson());//{ "$and" : [{ "$or" : [[{ "firstname" : "Pat" }, { "firstname" : "Matt" }]] }, { "$or" : [[{ "firstname" : "Pat" }, { "firstname" : "Matt" }]] }] }

List<Document> result = collectionMain.find(query).into(new ArrayList<Document>());

也应考虑在< <代码> > < /P>中使用<代码> $>或<代码> > 从:

使用$or时,将对 相同字段的值,请使用$in运算符而不是$or 接线员


在您的例子中,
Arrays.asList(List)
生成
List
。在您引用的示例中,它是
列表
。我会责怪的。将array.asList(docs)更改为docs消除了错误,但结果不接近(A或B)和(C或d)。有点像A或B或C或D。@saurav的建议是完整的。非常感谢。非常感谢您查看我的可耻代码并指出其他错误!非常感谢,索拉夫!很抱歉,这笔赏金不能分割。4J41-s的回答来得晚了一点,但更为个人化。我不能忽视这一点。
List<Document> docs = new ArrayList<>();

docs.add(new Document("lastname","Walker"));
docs.add(new Document("lastname","Harris"));    

Document queryLN = new Document("$or", Arrays.asList(docs));

docs.clear();

System.out.println(queryLN.toJson());//{ "$or" : [[]] } 

docs.add(new Document("firstname", "Pat"));
docs.add(new Document("firstname", "Matt"));

Document queryFN = new Document("$or", Arrays.asList(docs));

System.out.println(queryLN.toJson());//{ "$or" : [[{ "firstname" : "Pat" }, { "firstname" : "Matt" }]] }
System.out.println(queryFN.toJson());//{ "$or" : [[{ "firstname" : "Pat" }, { "firstname" : "Matt" }]] }

Document query = new Document("$and", Arrays.asList(queryFN, queryLN));

System.out.println(query.toJson());//{ "$and" : [{ "$or" : [[{ "firstname" : "Pat" }, { "firstname" : "Matt" }]] }, { "$or" : [[{ "firstname" : "Pat" }, { "firstname" : "Matt" }]] }] }

List<Document> result = collectionMain.find(query).into(new ArrayList<Document>());
List<Document> docsLN = new ArrayList<Document>();
List<Document> docsFN = new ArrayList<Document>();

for (Integer ln: input.getLastnames()) {
    docsLN.add(new Document("lastname",ln));
}           

Document queryLN = new Document("$or", docsLN);

for (Integer fn: input.getFirstnames()) {
    docsFN.add(new Document("firstname",fn));
}

Document queryFN = new Document("$or", docsFN);

System.out.println(queryLN.toJson());
System.out.println(queryFN.toJson());

Document query = new Document("$and", Arrays.asList(queryFN, queryLN));

System.out.println(query.toJson());
List<Document> result = collectionMain.find(query).into(new ArrayList<Document>());