mongodb - How can we write mongo db query in Spring -
i have feed collection has 2 fields: username , sentiment.  have write aggregation in spring group username , sentiment. have display total number of times username comes in collection. 
how that?
my code is:
db.feed.aggregate([ {$group: {_id : {username : '$username',sentiment:'$sentiment'}, total:{$sum             :1}}}, {$project : {username : '$_id.username', sentiment : '$_id.sentiment', total :                     '$total', _id : 0}} ]) 
the first thing comes mind:
http://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#mongo.aggregation
so in case like:
import static org.springframework.data.mongodb.core.aggregation.aggregation.*;  class stat {    string username;    string sentiment;    int total; }  aggregation agg = newaggregation(     group("username", "sentiment").count().as("total"),     project().        and("_id.username").as("username").        and("_id.sentiment").as("sentiment").        and("total").as("total") );  aggregationresults<stat> results = mongotemplate.aggregate(agg, "feed", stat.class); list<stat> mappedresult = results.getmappedresults(); 
Comments
Post a Comment