gpt4 book ai didi

java - MongoDB:在 Java 中使用 $gte 和 $lte 进行查询

转载 作者:可可西里 更新时间:2023-11-01 09:57:02 24 4
gpt4 key购买 nike

我想对大于或等于且小于或等于的字段执行查询(顺便说一句,我使用的是 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))));

有什么帮助吗?

最佳答案

基本上您需要这样的范围查询:

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:在 Java 中使用 $gte 和 $lte 进行查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32846996/

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