我在 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);