gpt4 book ai didi

java - MongoDB:合并两个 .find() 语句

转载 作者:行者123 更新时间:2023-12-02 04:20:03 25 4
gpt4 key购买 nike

使用Java。我有两个 .find() 查询,我想将它们组合起来并获取包含两个查询结果的文档。我已经成功地像这样单独创建它们。请注意,查询位于两个不同的顶级字段上。下面的最后一条语句是对同一字段的两个条件的查询。

FindIterable<Document> iterable = db.getCollection("1dag").find(new Document("id", "10"));

FindIterable<Document> iterable2 = db.getCollection("1dag").find(
new Document().append("timestamp", new Document()
.append("$gte",startTime)
.append("$lte",endTime)));

我找不到任何关于此的文档。这是我应该使用“$and”或“$where”语句的地方吗?

编辑这是这样做的方法吗?

FindIterable<Document> iterable7 = db.getCollection("1dag").find(
new Document()
.append("timestamp", new Document()
.append("$gte", startTime)
.append("$lte", endTime))
.append("id", new Document()
.append("$eq", 10)));

最佳答案

您的查询将完美运行。

查询db.inventory.find({id:{$eq:10}})相当于db.inventory.find({id: 10})

因此简化您的查询:

FindIterable<Document> iterable7 = db.getCollection("1dag").find(
new Document().append("timestamp", new Document()
.append("$gte", startTime)
.append("$lte", endTime))
.append("id",10));

关于java - MongoDB:合并两个 .find() 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32885027/

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