用于mongodb查询的Java查询代码

时间:2017-07-04 13:46:43

标签: java mongodb nosql

有人可以帮助将这个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);

1 个答案:

答案 0 :(得分:0)

我们能够转换上面的查询解决方案,这里是上面查询的代码,只是回答,如果有人遇到这种问题可以参考这个答案

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).append("tour.travelStatus",new BasicDBObject("$ne", "Unreceipted")));
     BasicDBList addition=new BasicDBList();
     addition.add("$bookingDate");
     addition.add(OperationsConstant.UTC_TO_IST_MILLISECONDS);
     BasicDBObject monthDoc=new BasicDBObject("$add",addition);
     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);