How to perform grouping with DBRef in Spring Data MongoDB -
i need aggregation number of comments related user.
the entity "comment entity" 1 has reference of user whom commentary directed.
@document(collection = commententity.collection_name) public class commententity { public final static string collection_name = "comments"; @id private objectid id; @field("message") private string message; @field("likes") private long likes = 0l; @field("created_time") private date createdtime; @field("target") @dbref private sonentity sonentity; public commententity() { } @persistenceconstructor public commententity(string message, date createdtime, sonentity sonentity) { this.message = message; this.createdtime = createdtime; this.sonentity = sonentity; } public objectid getid() { return id; } public void setid(objectid id) { this.id = id; } public string getmessage() { return message; } public void setmessage(string message) { this.message = message; } public long getlikes() { return likes; } public void setlikes(long likes) { this.likes = likes; } public sonentity getsonentity() { return sonentity; } public void setsonentity(sonentity sonentity) { this.sonentity = sonentity; } public date getcreatedtime() { return createdtime; } public void setcreatedtime(date createdtime) { this.createdtime = createdtime; } @override public string tostring() { return "commententity{" + "id=" + id + ", message=" + message + ", likes=" + likes + ", createdtime=" + createdtime + '}'; } }
i have implemented custom repository method following approach:
public class commentrepositoryimpl implements commentrepositorycustom { @autowired private mongotemplate mongotemplate; @override public list<commentsbysondto> getcommentsbyson() { typedaggregation<commententity> commentsaggregation = aggregation.newaggregation(commententity.class, aggregation.group("sonentity.firstname").count().as("comments"), aggregation.project("sonentity.firstname").and("comments").previousoperation()); aggregationresults<commentsbysondto> results = mongotemplate. aggregate(commentsaggregation, commentsbysondto.class); list<commentsbysondto> commentsbysonresultslist = results.getmappedresults(); return commentsbysonresultslist; } }
i did not work , not know how raise it.
the resulting dto follows:
public class commentsbysondto implements serializable { private string firstname; private long comments; public commentsbysondto(){} public commentsbysondto(string firstname, long commets) { this.firstname = firstname; this.comments = commets; } public string getfirstname() { return firstname; } public void setfirstname(string firstname) { this.firstname = firstname; } public long getcomments() { return comments; } public void setcomments(long comments) { this.comments = comments; } }
thanks in advance.
Comments
Post a Comment