Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Data MongoDB addToSet() with complex object

I want to get data from MongoDB through Java application using Spring Data.

I did following MongoDB query and converted successfully to Java code

db.getCollection('financialMessage').aggregate([{
            $match:{ createdDate: {
                       $gte: ISODate("2017-11-03 00:00:00.000Z"), 
                       $lt: ISODate("2017-11-04 00:00:00") }}}, { 
            $group: { _id: {
                        consolidatedBatchId: "$consolidatedBatchId",
                        version: "$version"},
                      messages: { $addToSet: "$message" }}}, {
            $sort: {
                    "_id.consolidatedBatchId": 1,
                    "_id.version": 1}
            }])

The results looks like :

{
    "_id" : {
        "consolidatedBatchId" : "5f4e1d16-2070-48ef-8369-00004ec3e8ee",
        "version" : 4
    },
    "messages" : [ 
        "message1", 
        "message2", 
        "message3"
    ]
}

Java code for above query looks like :

        Criteria filterCriteria = Criteria.where(CREATED_DATE)
            .gte(startDate)
            .lt(endDate);
        Sort sort = new Sort(Sort.Direction.DESC, "consolidatedBatchId" ,"version");

        Aggregation agg = Aggregation.newAggregation(
            Aggregation.match(filterCriteria),
            Aggregation.group("consolidatedBatchId", "version")
                .addToSet("message").as("messages"),
            Aggregation.sort(sort)
        );

        AggregationResults<FinancialMessageKey> aggregationResults =
            mongoTemplate.aggregate(agg, FinancialMessage.class, FinancialMessageKey.class);

        return aggregationResults.getMappedResults();

Now I do not find how to convert following MongoDB query code to Java code :

db.getCollection('financialMessage').aggregate([{
        $match:{ createdDate: {
                            $gte: ISODate("2017-11-03 00:00:00.000Z"), 
            $lt: ISODate("2017-11-04 00:00:00")
                }}}, { 
        $group: { _id: {
            consolidatedBatchId: "$consolidatedBatchId",
            version: "$version"},
                            messages: { $addToSet: {message: "$message",
                                             createdDate: "$createdDate",
                                             sender: "$sender",
                                             receiver: "$receiver" }}}}, {
        $sort: {
                "_id.consolidatedBatchId": 1,
                "_id.version": 1}
        }])

With the following output :

{
    "_id" : {
        "consolidatedBatchId" : "5f4e1d16-2070-48ef-8369-00004ec3e8ee",
        "version" : 4
    },
    "messages" : [ 
        {
            "message" : "message1",
            "createdDate" : ISODate("2017-11-03T07:13:08.074Z"),
            "sender" : "sender",
            "receiver" : "receiver"
        }, 
        {
            "message" : "message2",
            "createdDate" : ISODate("2017-11-03T07:13:08.111Z"),
            "sender" : "sender",
            "receiver" : "receiver"
        }, 
        {
            "message" : "message3",
            "createdDate" : ISODate("2017-11-03T07:13:07.986Z"),
            "sender" : "sender",
            "receiver" : "receiver"
        }
    ]
}

How this addToSet() could be write in Java in order to get List<'complex object'> instead of simple List ?

like image 992
Florin Lei Avatar asked Mar 08 '23 16:03

Florin Lei


1 Answers

After few minutes of research on Google, I finally found how to do it.

public List<FinancialMessageKey> findFinancialMessageKeys(FinancialMessageQueryDTO financialMessageQueryDTO) {

        Criteria filterCriteria = Criteria.where(CREATED_DATE)
            .gte(startDate)
            .lt(endDate);
        Sort sort = new Sort(Sort.Direction.DESC, "consolidatedBatchId", "version");

        Aggregation agg = Aggregation.newAggregation(
            Aggregation.match(filterCriteria),
            Aggregation.group(CONSOLIDATED_BATCH_ID, VERSION)
                .addToSet(new BasicDBObject() {
                              {
                                  put(CREATED_DATE, "$" + CREATED_DATE);
                                  put(SENDER, "$" + SENDER);
                                  put(RECEIVER, "$" + RECEIVER);
                                  put(MESSAGE, "$" + MESSAGE);
                              }
                          }
                ).as("messages"),
            Aggregation.sort(sort));

        AggregationResults<FinancialMessageKey> aggregationResults =
            mongoTemplate.aggregate(agg, FinancialMessage.class, FinancialMessageKey.class);

        return aggregationResults.getMappedResults();
    }
like image 51
Florin Lei Avatar answered Mar 10 '23 11:03

Florin Lei