gpt4 book ai didi

java - 如何查找其数组字段仅包含两个特定值的所有文档

转载 作者:行者123 更新时间:2023-12-02 13:35:32 26 4
gpt4 key购买 nike

我这样做了,但它返回了其作者字段包含两个特定值和其他值的所有文档。

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com