Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MongoDB:在java中使用$gte和$lte进行查询_Java_Mongodb_Querying - Fatal编程技术网

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

MongoDB:在java中使用$gte和$lte进行查询,java,mongodb,querying,Java,Mongodb,Querying,我想对大于或等于、小于或等于的字段执行查询(我使用的是java btw)。换句话说而且构造函数新文档(key,value)只为您获取一个具有一个键值对的文档。但是在这种情况下,您需要创建一个包含多个文档的文档。为此,请创建一个空文档,然后使用.append(key,value)向其添加对 documenttimespan=newdocument(); 时间跨度追加(“$gt”,1412204098); 时间跨度追加(“$lt”,1412204998); //JSON中的时间跨度: //{$gt:

我想对大于或等于、小于或等于的字段执行查询(我使用的是java btw)。换句话说而且构造函数
新文档(key,value)
只为您获取一个具有一个键值对的文档。但是在这种情况下,您需要创建一个包含多个文档的文档。为此,请创建一个空文档,然后使用
.append(key,value)
向其添加对

documenttimespan=newdocument();
时间跨度追加(“$gt”,1412204098);
时间跨度追加(“$lt”,1412204998);
//JSON中的时间跨度:
//{$gt:1412204098,$lt:1412204998}
文档条件=新文档(“时间戳”,时间跨度);
//JSON中的条件:
//{时间戳:{$gt:1412204098,$lt:1412204998}
FindItemerable-iterable=db.getCollection(“1dag”).find(条件);
或者,如果您确实希望使用不带临时变量的单行程序:

FindIterable<Document> iterable = db.getCollection("1dag").find(
    new Document()
        .append("timestamp", new Document()
             .append("$gt",1412204098)
             .append("$lt",1412204998)
        )
);
FindIterable-iterable=db.getCollection(“1dag”).find(
新文件()
.append(“时间戳”,新文档()
.附加(“$gt”,1412204098)
.附加($lt),1412204998)
)
);

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

db.getCollection("1dag").find({
    "timestamp": {
        "$gte": 1412204098,
        "$lte": 1412204099
    }
})
由于此范围查询需要多个查询条件,因此可以通过使用方法将条件附加到查询文档中来指定逻辑连词(AND):

FindIterable<Document> iterable = db.getCollection("1dag").find(
        new Document("timestamp", new Document("$gte", 1412204098).append("$lte", 1412204099)));
FindIterable-iterable=db.getCollection(“1dag”).find(
新文档(“时间戳”,新文档($gte),1412204098)。追加($lte,1412204099));

您的一行程序有语法错误,但您的解决方案有效。尽管运行时间比公认的答案慢。无论如何谢谢你@kongshem你能建议编辑来修复那些语法错误吗?我在考虑,但似乎是运算符“:”引起了麻烦。或者使用.append函数创建空文档。实际上,它是“:”运算符。使用“解决方案”。我的数据收集的运行时间仍然慢半秒。(700MB的测试数据,我将在高达1TB的数据上使用)。我将编辑您的回复。
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
    }
})
FindIterable<Document> iterable = db.getCollection("1dag").find(
        new Document("timestamp", new Document("$gte", 1412204098).append("$lte", 1412204099)));