[英]MongoDB: Combine two .find() statements
Using Java. 使用Java。 I have two .find() queries that I want to combine and get a Document containing the results of both queries.
我有两个要合并的.find()查询,并获得一个包含两个查询结果的Document。 I have managed to create them individually like this.
我设法这样单独创建它们。 Notice that the queries are on two different top-level fields.
请注意,查询位于两个不同的顶级字段上。 The last statement below is a query on the same field with two conditions.
下面的最后一条语句是对具有两个条件的同一字段的查询。
FindIterable<Document> iterable = db.getCollection("1dag").find(new Document("id", "10"));
and 和
FindIterable<Document> iterable2 = db.getCollection("1dag").find(
new Document().append("timestamp", new Document()
.append("$gte",startTime)
.append("$lte",endTime)));
I can't find any documentation on this. 我找不到与此有关的任何文档。 Is this where I should use the "$and" or "$where" statements?
这是我应该在其中使用“ $ and”或“ $ where”语句的地方吗?
EDIT is this the way to do it? 编辑是这样做的方法吗?
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)));
Your query will work perfectly. 您的查询将完美运行。
The query db.inventory.find({id:{$eq:10}})
is equivalent to db.inventory.find({id: 10})
查询
db.inventory.find({id:{$eq:10}})
等同于db.inventory.find({id: 10})
So simplifying your query: 因此,简化查询:
FindIterable<Document> iterable7 = db.getCollection("1dag").find(
new Document().append("timestamp", new Document()
.append("$gte", startTime)
.append("$lte", endTime))
.append("id",10));
To create the equivalent Java query for the following mongo shell query 为以下mongo shell查询创建等效的Java查询
db.getCollection("1dag").find({
"id": "10",
"timestamp": {
"$gte": 1412204098,
"$lte": 1412204099
}
})
you should specify a logical conjunction (AND) for multiple query conditions by appending conditions to the query document: 您应该通过将条件附加到查询文档中来为多个查询条件指定逻辑并(AND) :
FindIterable<Document> iterable = db.getCollection("1dag").find(
new Document("id", "10")
.append("timestamp",
new Document("$gte", 1412204098)
.append("$lte", 1412204099)
)
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.