Java中的Mongodb聚合日期匹配

时间:2018-01-08 17:27:21

标签: java mongodb aggregation-framework mongodb-java mongodb-java-3.3.0

我有以下Mongo DB聚合查询,它给出了以下输出

Mongodb中的示例文档

/* 4 */
{
    "_id" : ObjectId("5a536d89e5b8f73d4e41ba96"),
    "name" : "yyyyy",
    "creationDate" : ISODate("2018-01-02T16:27:25.201Z"),
    "address" : "xxx",
    "zipcode" : "10254"
}

/* 5 */
{
    "_id" : ObjectId("5a536d95e5b8f73d4e41ba97"),
    "name" : "zzzzz",
    "creationDate" : ISODate("2018-01-03T16:28:25.201Z"),
    "address" : "xxx",
    "zipcode" : "10254"
}

汇总查询

db.test_customer.aggregate([
    {$match:{creationDate:{"$gte":ISODate("2018-01-01"),"$lt":ISODate("2018-01-05")}}},
    {$project:{"_id":"$_id","name":"$name",creationDate:"$creationDate"}},
    {$group:{"_id":"$_id",customer:{"$push":"$$ROOT"}}}
    ])

这给了我以下结果

/* 1 */
{
    "_id" : ObjectId("5a536d95e5b8f73d4e41ba97"),
    "customer" : [ 
        {
            "_id" : ObjectId("5a536d95e5b8f73d4e41ba97"),
            "name" : "zzzzz",
            "creationDate" : ISODate("2018-01-03T16:28:25.201Z")
        }
    ]
}

/* 2 */
{
    "_id" : ObjectId("5a536d89e5b8f73d4e41ba96"),
    "customer" : [ 
        {
            "_id" : ObjectId("5a536d89e5b8f73d4e41ba96"),
            "name" : "yyyyy",
            "creationDate" : ISODate("2018-01-02T16:27:25.201Z")
        }
    ]
}

当我尝试转换为Java编码时,我没有得到输出。它返回空结果

我的Java代码

DBCollection collection = mongoTemplate.getCollection("test_customer");
        DBObject match = new BasicDBObject("$match", new BasicDBObject("creationDate", new BasicDBObject("$gte", getDate("01/01/2018")).append("$lt", getDate("05/01/2018"))));
        DBObject project = new BasicDBObject("$project", new BasicDBObject("_id", "$_id").append("name", "$name").append("creationDate", "$creationDate"));
        DBObject group = new BasicDBObject("$group", new BasicDBObject("_id", "$_id").append("customer", new BasicDBObject("$push", "$$ROOT")));
        AggregationOutput aggregate = collection.aggregate(Arrays.asList(match, project, group));
        Iterable<DBObject> results = aggregate.results();
        for (DBObject obj : results) {
            String obj1 = (String) obj.toString();
            System.out.println(obj1);
        }

private Date getDate(String date) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
            sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
            Date date1 = sdf.parse(date);
            return date1;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

我的Java代码正在创建如下所示的聚合查询

db.test_customer.aggregate([
    { "$match" : { "creationDate" : { "$gte" : { "$date" : "2017-12-31T18:30:00.000Z"} , "$lt" : { "$date" : "2018-01-04T18:30:00.000Z"}}}},
    { "$project" : { "_id" : "$_id" , "name" : "$name" , "creationDate" : "$creationDate"}},
    { "$group" : { "_id" : "$_id" , "customer" : { "$push" : "$$ROOT"}}}
    ])

我不知道为什么$ date没有在mongodb中选择ISODate。请建议我如何在mongodb中使用聚合日期条件

0 个答案:

没有答案