0

I have data in mongodb in following format. i presented below two row of my mongodb data of table named 'feed'.

{

"_id" : ObjectId("55e80a725ae35bbfc6dce074"),
"sourceId" : "12345",
"sourceName" : "MyPage",
"channelId" : "67890",
"channelName" : "Facebook",
"likeCount" : 1,
"feedData" : {
    "from" : {
        "id" : "416992615172913",
        "name" : "Ashish Kumar"
    },
    "created_time" : "2015-08-04T09:54:39+0000",
},
"sentiment" : "neutral",

}

{

"_id" : ObjectId("55e80a725ae35bbfc6dce074"),
"sourceId" : "23456",
"sourceName" : "YourPage",
"channelId" : "67890",
"channelName" : "Facebook",
"likeCount" : 2,
"feedData" : {
    "from" : {
        "id" : "416992615172913",
        "name" : "Ashish Kumar"
    },
    "created_time" : "2015-08-04T09:54:39+0000",
},
"sentiment" : "positive",

}

I want to perform groupBy on field feedData.from.id where channelName is equal to 'Facebook', sum of like count, all sources in an array, all sentiment in an array. i want all data associated with feedData.from.id in the format given below using spring mongo.

{

"feedDatafromid" : "416992615172913",

"name" : "Ashish Kumar",

"sourceName" : ["MyPage","YourPage"],

"channel" : "Facebook",

"likeCount" : 3,

"sentiment" : ["neutral","positive"]

}

Can someone help me to write code in java Spring to group the data of mongodb in given manner.

1 Answers1

0

Suppose we have FeedDbBean class to save the result of aggregation.

Aggregation agg = newAggregation(match(criteria),group(userIdKey).sum("likeCount").as("likeCount").sum("commentCount").as("commentCount").addToSet("sourceName").as("sourceName").addToSet("sourceId").as("sourceId").addToSet("sentiment").as("sentiment").addToSet("channelId").as("channelId").addToSet("channelName").as("channelName").addToSet(userIdKey).as("postUserId").addToSet(userNameKey).as("postUserName"));

results = mongoTemplate.aggregate(agg, FEED_COLLECTION_NAME, FeedDbBean.class);

if(null!=results) List feedList = results.getMappedResults();

...................

so using given code the result on behalf of userIdKey will be stored in list of FeedDbBean.

each FeedDbBean object will have property like following manner.

feedDbBean.userId = (userId)

feedDbBean.userName = (userName)

feedDbBean.likeCount = (sum of like count group by userId)

feedDbBean.commentCount =(sum of comment count groupby userid)

feedDbBean.sourceName = (all unique source name group by userid show here separated by comma)

feedDbBean.sentiment = (all sentiment separated by comma group by userid)