P.K.Hindustani
P.K.Hindustani

Reputation: 148

How to get aggregated result on the basis of "dd-MM-yyyy" using spring data mongodb

Every document contains different datetime but I want to aggregate on the basis of date only.

/* 1 createdAt:10/3/2017, 11:44:51 AM*/
{
    "_id" : ObjectId("59d32adb816e3e567910a4c5"),
    "_class" : "com.mongo.test.MongoData",
    "date" : ISODate("2017-10-03T11:44:51.127+05:30"),
    "name" : "frji-1"
},

/* 2 createdAt:10/3/2017, 11:44:51 AM*/
{
    "_id" : ObjectId("59d32adb816e3e567910a4c6"),
    "_class" : "com.mongo.test.MongoData",
    "date" : ISODate("2017-10-03T11:44:51.127+05:30"),
    "name" : "frji-2"
},

/* 3 createdAt:10/3/2017, 11:44:51 AM*/
{
    "_id" : ObjectId("59d32adb816e3e567910a4c7"),
    "_class" : "com.mongo.test.MongoData",
    "date" : ISODate("2017-10-03T11:44:51.127+05:30"),
    "name" : "frji-3"
},

/* 4 createdAt:10/3/2017, 11:44:51 AM*/
{
    "_id" : ObjectId("59d32adb816e3e567910a4c8"),
    "_class" : "com.mongo.test.MongoData",
    "date" : ISODate("2017-10-03T11:44:51.127+05:30"),
    "name" : "frji-4"
},

/* 5 createdAt:10/3/2017, 11:44:51 AM*/
{
    "_id" : ObjectId("59d32adb816e3e567910a4c9"),
    "_class" : "com.mongo.test.MongoData",
    "date" : ISODate("2017-10-03T11:44:51.127+05:30"),
    "name" : "frji-5"
},

/* 6 createdAt:10/3/2017, 11:44:51 AM*/
{
    "_id" : ObjectId("59d32adb816e3e567910a4ca"),
    "_class" : "com.mongo.test.MongoData",
    "date" : ISODate("2017-10-04T01:44:51.127+05:30"),
    "name" : "frji-6"
},

/* 7 createdAt:10/3/2017, 11:44:51 AM*/
{
    "_id" : ObjectId("59d32adb816e3e567910a4cb"),
    "_class" : "com.mongo.test.MongoData",
    "date" : ISODate("2017-10-04T01:44:51.127+05:30"),
    "name" : "frji-7"
},

/* 8 createdAt:10/3/2017, 11:44:51 AM*/
{
    "_id" : ObjectId("59d32adb816e3e567910a4cc"),
    "_class" : "com.mongo.test.MongoData",
    "date" : ISODate("2017-10-04T01:44:51.127+05:30"),
    "name" : "frji-8"
},

/* 9 createdAt:10/3/2017, 11:44:51 AM*/
{
    "_id" : ObjectId("59d32adb816e3e567910a4cd"),
    "_class" : "com.mongo.test.MongoData",
    "date" : ISODate("2017-10-04T01:44:51.127+05:30"),
    "name" : "frji-9"
},

/* 10 createdAt:10/3/2017, 11:44:51 AM*/
{
    "_id" : ObjectId("59d32adb816e3e567910a4ce"),
    "_class" : "com.mongo.test.MongoData",
    "date" : ISODate("2017-10-04T01:44:51.127+05:30"),
    "name" : "frji-10"
},

/* 11 createdAt:10/3/2017, 11:44:51 AM*/
{
    "_id" : ObjectId("59d32adb816e3e567910a4cf"),
    "_class" : "com.mongo.test.MongoData",
    "date" : ISODate("2017-10-02T11:44:51.127+05:30"),
    "name" : "frji-11"
},

/* 12 createdAt:10/3/2017, 11:44:51 AM*/
{
    "_id" : ObjectId("59d32adb816e3e567910a4d0"),
    "_class" : "com.mongo.test.MongoData",
    "date" : ISODate("2017-10-02T11:44:51.127+05:30"),
    "name" : "frji-12"
},

/* 13 createdAt:10/3/2017, 11:44:51 AM*/
{
    "_id" : ObjectId("59d32adb816e3e567910a4d1"),
    "_class" : "com.mongo.test.MongoData",
    "date" : ISODate("2017-10-02T11:44:51.127+05:30"),
    "name" : "frji-13"
},

/* 14 createdAt:10/3/2017, 11:44:51 AM*/
{
    "_id" : ObjectId("59d32adb816e3e567910a4d2"),
    "_class" : "com.mongo.test.MongoData",
    "date" : ISODate("2017-10-02T11:44:51.127+05:30"),
    "name" : "frji-14"
},

/* 15 createdAt:10/3/2017, 11:44:51 AM*/
{
    "_id" : ObjectId("59d32adb816e3e567910a4d3"),
    "_class" : "com.mongo.test.MongoData",
    "date" : ISODate("2017-10-02T11:44:51.127+05:30"),
    "name" : "frji-15"
}

Entity class is to communicate with mongo using spring-data-mongodb:

@Document(collection="integer_test")
public class MongoData {
    @Id
    private String id;
    private Date
    date;
    private String name;
    @Override
    public String toString() {
        return "MongoData [date=" + date + ", id=" + id + "]";
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Date getDate() {
        return date;
    }
    public void setDate(Date date) {
        this.date = date;
    }

}

Aggregated data result class:

public class AggData {

    private Date date;
    private int total;

    public Date getDate() {
        return date;
    }


    public void setDate(Date date) {
        this.date = date;
    }


    public int getTotal() {
        return total;
    }


    public void setTotal(int total) {
        this.total = total;
    }
}

Then how to get aggregated result into AggData class using spring-data-mongodb? I am using mongotemplate to communicate with db.

Upvotes: 1

Views: 370

Answers (1)

Devendra Singh
Devendra Singh

Reputation: 640

This is you can try with Mongotemplate

GroupOperation groupOperation=Aggregation.group("date").first(StringOperators.Substr.valueOf("date").substring(0, 10)).as("date").count().as("total");

  Aggregation aggregation=Aggregation.newAggregation(groupOperation);
  AggregationResults<AggData> aggregationResults=mongoTemplate.aggregate(aggregation, "integer_test",AggData.class);

Upvotes: 1

Related Questions