Java 如何查找其数组字段仅包含两个特定值的所有文档
我这样做了,但它会返回其作者字段包含两个特定值和其他值的所有文档Java 如何查找其数组字段仅包含两个特定值的所有文档,java,mongodb,Java,Mongodb,我这样做了,但它会返回其作者字段包含两个特定值和其他值的所有文档 Document authors = new Document("authors","firstValue") .append("authors", "secondValue"); MongoCursor<Document> cursor = collection.find(authors).iterator(); try { while (cursor.hasNext())
Document authors = new Document("authors","firstValue")
.append("authors", "secondValue");
MongoCursor<Document> cursor = collection.find(authors).iterator();
try {
while (cursor.hasNext()) {
Document doc = cursor.next();
System.out.println(doc.get("title")+ " " + doc.get("authors"));
}
}
finally {
cursor.close();
}
client.close();
documentauthors=新文档(“authors”,“firstValue”)
.附加(“作者”、“第二价值”);
MongoCursor=collection.find(authors.iterator();
试一试{
while(cursor.hasNext()){
Document doc=cursor.next();
System.out.println(doc.get(“title”)+“”+doc.get(“authors”);
}
}
最后{
cursor.close();
}
client.close();
我只想找到只包含这两个值的文档。您通过将
secondValue
附加到authors
键来覆盖这些值。因此过滤器看起来像{“authors”:“secondValue”}
因此,如果您需要仅包含两个值(包括顺序)的数组文档,则需要一个过滤器,如{“authors”:[“firstValue”,secondValue“]}
documentauthors=新文档(“authors”,Arrays.asList(“firstValue”,“secondValue”))