我这样做了,但它返回了其作者字段包含两个特定值和其他值的所有文档。
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();
我只想查找仅包含这两个值的文档。
最佳答案
您将通过将 secondValue
附加到 authors
键来覆盖这些值。因此过滤器看起来像 { "authors": "secondValue"}
。
因此,如果您需要包含仅包含两个值(包括顺序)的数组的文档,则需要像 { "authors": [ "firstValue", secondaryValue"] }
文档作者 = new Document("authors", Arrays.asList("firstValue", "secondValue"))
;
关于java - 如何查找其数组字段仅包含两个特定值的所有文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43021328/