Mongodb Aggregate Date match in Java

2.4k views Asked by At

I have below Mongo DB aggregate query which is giving me the below output

Sample Documents in 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"
}

Aggregate Query

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"}}}
    ])

This gives me below result

/* 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")
        }
    ]
}

When i try to convert to Java coding i am not getting the output. it is returning empty result

My Java code

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;
    }

My Java code is creating aggregate query like below

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"}}}
    ])

I don't know why $date is not picking the ISODate in mongodb. Please suggest me how to use aggregate date condition in mongodb

0

There are 0 answers