MongoDB:组合两个 .find() 语句
MongoDB: Combine two .find() statements
使用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”语句的地方吗?
EDIT 这是方法吗?
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));
为以下 mongo shell 查询
创建等效的 Java 查询
db.getCollection("1dag").find({
"id": "10",
"timestamp": {
"$gte": 1412204098,
"$lte": 1412204099
}
})
您应该通过向查询文档附加条件来为多个查询条件指定一个logical conjunction (AND):
FindIterable<Document> iterable = db.getCollection("1dag").find(
new Document("id", "10")
.append("timestamp",
new Document("$gte", 1412204098)
.append("$lte", 1412204099)
)
);
使用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”语句的地方吗?
EDIT 这是方法吗?
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));
为以下 mongo shell 查询
创建等效的 Java 查询db.getCollection("1dag").find({
"id": "10",
"timestamp": {
"$gte": 1412204098,
"$lte": 1412204099
}
})
您应该通过向查询文档附加条件来为多个查询条件指定一个logical conjunction (AND):
FindIterable<Document> iterable = db.getCollection("1dag").find(
new Document("id", "10")
.append("timestamp",
new Document("$gte", 1412204098)
.append("$lte", 1412204099)
)
);