【发布时间】:2017-07-08 13:33:52
【问题描述】:
有人可以帮助将此 mongo 查询转换为 java 代码吗? 它在 mongo 命令行中运行良好,但我无法使用 java 运行它。 这是mongo查询
db.booking.aggregate([{
"$match": {
"bookingDate": {
"$ne": null,
"$gte": new Date("2017-04-01"),
"$lte": new Date("2018-03-31")
}
}
},
{
"$project": {
"totalAmount": 1,
"totalPax": 1,
"month": {
"$month": {
$add: ["$bookingDate", 25200000]
}
}
}
},
{
"$group": {
"_id": "$month",
"totalPax": {
"$sum": "$totalPax"
},
"totalAmount": {
"$sum": "$totalAmount"
},
}
}
])
这是我尝试过的,但它不起作用,
BasicDBObject match = new BasicDBObject("$match", new BasicDBObject("bookingDate",
new BasicDBObject("$gte", OperationsUtil.getISODateFromUIDate(startDate)).append("$lte",OperationsUtil.getISODateFromUIDate(endDate)).append("$ne", null)));
BasicDBObject match1 = new BasicDBObject("$match", new BasicDBObject("bookingDeleted",false));
BasicDBList add=new BasicDBList();
add.add(new BasicDBObject("$bookingDate",25200000));
BasicDBObject monthDoc=new BasicDBObject("$add",add);
BasicDBObject project = new BasicDBObject("$project",new BasicDBObject("_id",0).append("totalAmount",1).append("totalPax",1).append("month",new BasicDBObject("$month",monthDoc)));
DBObject group = new BasicDBObject("$group", new BasicDBObject("_id","$month")
.append("totalPaxCount", new BasicDBObject("$sum", "$totalPax")).append("totalAmount",new BasicDBObject("$sum", "$totalAmount")));
DBObject sort = new BasicDBObject("$sort", new BasicDBObject("_id", 1));
AggregationOutput cursor = bookingCollection.aggregate(match,match1,project,group,sort);
【问题讨论】:
-
你试过什么?这真的就像在看到
{}和List时使用Document一样简单,无论您在哪里看到[]。就是这样。 -
@NeilLunn 用 java 尝试过的代码编辑了问题