-1

我在 MySQL 中有一个包含以下条件的查询:

WHERE START_TIME < ? AND START_TIME+DURATION >= ?

我应该如何使用 Java 驱动程序和聚合框架将其迁移到 MongoDB?第一个条件将变为:

DBObject match = new BasicDBObject("$match", new BasicDBObject("start_time", "{ $lt : "+timestamp+"}") );

但我不确定第二个。谢谢。

编辑
我尝试使用 Asya Kamsky 的回答,这就是我得到的,但它不起作用:

BasicDBList dbList = new BasicDBList();
dbList.add("$start_time");
dbList.add("$duration");

DBObject matchLT = new BasicDBObject("$match", new BasicDBObject("start_time", new BasicDBObject("$lt",timestamp)));
DBObject project = new BasicDBObject("$project", new BasicDBObject("end_time", new BasicDBObject("$add", dbList)));
DBObject matchGTE = new BasicDBObject("$match", new BasicDBObject("end_time", new BasicDBObject("$gte",timestamp)));
//GROUP CODE GOES HERE
AggregationOutput output = collection.aggregate(matchLT, project, matchGTE, group);
4

1 回答 1

1

以下是您在聚合框架中的操作方式,我相信您可以将其转换为 Java:

db.collection.aggregate([ {$match: {start_time:{$lt:ISODate("xxxx-xx-xx")}}},
           {$project:{end_time:{$add:["$start_time","$duration"]}}},
           {$match:{end_time:{$gt:ISODate("yyyy-yy-yy")}}}
] );
于 2013-11-10T21:56:50.410 回答