有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

mongodb查询的Java查询代码

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

共 (0) 个答案