MongoDB: 在 java 中使用 $gte 和 $lte 进行查询
MongoDB: Query using $gte and $lte in java
我想对大于或等于且小于或等于的字段执行查询(顺便说一句,我使用的是 java)。换一种说法。 >= 和 <=。据我了解,mongoDB 有 $gte 和 $lte 运算符,但我找不到使用它的正确语法。我正在访问的字段是顶级字段。
我已经设法让它工作了:
FindIterable<Document> iterable = db.getCollection("1dag").find(new Document("timestamp", new Document("$gt", 1412204098)));
还有...
FindIterable<Document> iterable = db.getCollection("1dag").find(new Document("timestamp", new Document("$lt", 1412204098)));
但是如何将它们相互结合起来呢?
目前我正在尝试这样的语句,但它不起作用:
FindIterable<Document> iterable5 = db.getCollection("1dag").find(new Document( "timestamp", new Document("$gte", 1412204098).append("timestamp", new Document("$lte",1412204099))));
有什么帮助吗?
构造函数new Document(key, value)
只会为您提供一个具有一个键值对的文档。但在这种情况下,您需要创建一个包含多个文档的文档。为此,创建一个空文档,然后使用 .append(key, value)
.
添加对
Document timespan = new Document();
timespan.append("$gt", 1412204098);
timespan.append("$lt", 1412204998);
// timespan in JSON:
// { $gt: 1412204098, $lt: 1412204998}
Document condition = new Document("timestamp", timespan);
// condition in JSON:
// { timestamp: { $gt: 1412204098, $lt: 1412204998} }
FindIterable<Document> iterable = db.getCollection("1dag").find(condition);
或者如果你真的想用一个没有临时变量的单行代码来做到这一点:
FindIterable<Document> iterable = db.getCollection("1dag").find(
new Document()
.append("timestamp", new Document()
.append("$gt",1412204098)
.append("$lt",1412204998)
)
);
基本上你需要这样的范围查询:
db.getCollection("1dag").find({
"timestamp": {
"$gte": 1412204098,
"$lte": 1412204099
}
})
由于此范围查询需要多个查询条件,可以通过append()
在查询文档中附加条件来指定逻辑合取(AND)方法:
FindIterable<Document> iterable = db.getCollection("1dag").find(
new Document("timestamp", new Document("$gte", 1412204098).append("$lte", 1412204099)));
我想对大于或等于且小于或等于的字段执行查询(顺便说一句,我使用的是 java)。换一种说法。 >= 和 <=。据我了解,mongoDB 有 $gte 和 $lte 运算符,但我找不到使用它的正确语法。我正在访问的字段是顶级字段。
我已经设法让它工作了:
FindIterable<Document> iterable = db.getCollection("1dag").find(new Document("timestamp", new Document("$gt", 1412204098)));
还有...
FindIterable<Document> iterable = db.getCollection("1dag").find(new Document("timestamp", new Document("$lt", 1412204098)));
但是如何将它们相互结合起来呢?
目前我正在尝试这样的语句,但它不起作用:
FindIterable<Document> iterable5 = db.getCollection("1dag").find(new Document( "timestamp", new Document("$gte", 1412204098).append("timestamp", new Document("$lte",1412204099))));
有什么帮助吗?
构造函数new Document(key, value)
只会为您提供一个具有一个键值对的文档。但在这种情况下,您需要创建一个包含多个文档的文档。为此,创建一个空文档,然后使用 .append(key, value)
.
Document timespan = new Document();
timespan.append("$gt", 1412204098);
timespan.append("$lt", 1412204998);
// timespan in JSON:
// { $gt: 1412204098, $lt: 1412204998}
Document condition = new Document("timestamp", timespan);
// condition in JSON:
// { timestamp: { $gt: 1412204098, $lt: 1412204998} }
FindIterable<Document> iterable = db.getCollection("1dag").find(condition);
或者如果你真的想用一个没有临时变量的单行代码来做到这一点:
FindIterable<Document> iterable = db.getCollection("1dag").find(
new Document()
.append("timestamp", new Document()
.append("$gt",1412204098)
.append("$lt",1412204998)
)
);
基本上你需要这样的范围查询:
db.getCollection("1dag").find({
"timestamp": {
"$gte": 1412204098,
"$lte": 1412204099
}
})
由于此范围查询需要多个查询条件,可以通过append()
在查询文档中附加条件来指定逻辑合取(AND)方法:
FindIterable<Document> iterable = db.getCollection("1dag").find(
new Document("timestamp", new Document("$gte", 1412204098).append("$lte", 1412204099)));