Sorting MongoDb java异步驱动程序:对集合中的值进行排序和限制的正确查询是什么

Sorting MongoDb java异步驱动程序:对集合中的值进行排序和限制的正确查询是什么,sorting,asynchronous,driver,mongodb-java,Sorting,Asynchronous,Driver,Mongodb Java,我使用的不是默认的驱动程序 我有一个db“test”,集合为“collect”,每个文档的格式如下,其中时间是10位unix时间戳 { "_id", "userId", "programId", "time" } 我想编写一个与下面给出的sql查询等价的查询,其中input1是unix时间戳格式的当前时间,input2是作为输入的用户ID SELECT * FROM collect WHERE time >= input1 AND userId = input2 ORDER BY

我使用的不是默认的驱动程序

我有一个db“test”,集合为“collect”,每个文档的格式如下,其中时间是10位unix时间戳

{ "_id", "userId", "programId", "time" }
我想编写一个与下面给出的sql查询等价的查询,其中input1是unix时间戳格式的当前时间,input2是作为输入的用户ID

SELECT * 
FROM collect 
WHERE time >= input1 AND userId = input2 
ORDER BY time DESC 
LIMIT 30
我做了这样的事

collection = db.getCollection("ProgramBookings"); Long Datetimesatmp =
new Date().getTime() / 1000;

Aggregate.Builder builder = new Aggregate.Builder();

builder = (builder.match(where("time").
          greaterThan(Datetimesatmp.toString()).and("userId")     .equals(userId.toString())).sort(desc("time"))).limit(30);

Aggregate d1 = builder.build();
在这里,我只想检索遵循标准的列表“时间”。但我被困在这里,在谷歌搜索后找不到多少有用的链接。我指的是做上述给定的代码。有没有一个简单的方法可以做到这一点

编辑: 我想将这些值添加到ProgramTime对象的列表中,如下所示

public class ProgramTime {

    private Integer userId;     
      private Integer programId;    
      private Long time; 
}

我不认为聚合框架是这里的正确选择。我会直接做一个“查找”。有一个Find类和嵌套的Find.Builder类用于构造更复杂的查询

导入静态com.allanbank.mongodb.builder.QueryBuilder.where; 导入com.allanbank.mongodb.MongoClient; import com.allanbank.mongodb.MongoCollection; import com.allanbank.mongodb.MongoFactory; 进口com.allanbank.mongodb.MongoIterator; 导入com.allanbank.mongodb.bson.Document; 导入com.allanbank.mongodb.builder.Find; 导入com.allanbank.mongodb.builder.Sort; 公共类堆栈溢出{ //从collect中选择* //其中时间>=input1,用户ID=input2 //按时间顺序描述 //限制30 公共静态void querylong input1,字符串input2{ MongoClient客户端=MongoFactory createClie先生ntmongodb://localhost:27017/; //选择*从收集-有点。。。 MongoCollection collection=client.getDatabasetest.getCollection 收集 Find.Builder=Find.Builder; //其中时间>=input1,用户ID=input2 builder.querywheretime.greaterThaninput1.anduserId .相等输入2; //按时间顺序描述 builder.sortSort.desctime; //限制30 builder.limit30; 请尝试MongoInterator iter=collection.findbuilder{ 文件编号:iter{ System.out.printlndoc; } } } }
谢谢@RobMoore,我将尝试一下。嗨@RobMoore,如果查询像SELECT*FROM collect WHERE time>=input1和userId IN 1,2,3。。。按时间顺序描述限制30。我怎样才能包括在1,2,3。。。而不是=input2。您可以在查询生成器上使用“in”方法:-例如….和userid.inExpressions.constant1、Expressions.constant12、Expressions.constant3