我认为这是一个很好的问题和答案,所以我决定在 java 中使用 Spring Data 和 MongoDB 来解决这个问题。要将 Asya 的答案转换为 java mongodb 代码,我执行了以下操作:
public void commentTest() {
BasicDBObject o1 = new BasicDBObject();
o1.append("c", "$commentor");
o1.append("i", "$commentid");
Aggregation aggCount = newAggregation(
project("commentid", "commentor")
.andExpression("concat(\"$strip\",\"-\",\"$yy\",\"/\",\"$mo\",\"/\",\"$da\")").as("strip"),
group("strip").push(o1).as("comms").max("commentid").as("max"),
match(Criteria.where("comms.c").is("Simon")),
unwind("comms"),
match(Criteria.where("comms.c").is("Simon")));
logger.info(aggCount.toString());
AggregationResults<CommentTest> groupCount = mongoTemplate.aggregate(aggCount, "commenttest", CommentTest.class);
List<CommentTest> resultCount = groupCount.getMappedResults();
ObjectMapper mapper = new ObjectMapper();
try {
logger.info(mapper.writeValueAsString(resultCount));
} catch (IOException e) {
e.printStackTrace();
}
}
然后为了让 mongotemplate 成功地将结果解析到CommentTest
类中,我必须创建一个对结果进行最小化的类:
Document(collection = "commenttest")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class CommentTest {
private String id, body, commentid, commentor, commentorid, da, filename, mo, strip, stripname, time, yy, max;
@JsonProperty
private comms comms;
public CommentTest.comms getComms() {
return comms;
}
public void setComms(CommentTest.comms comms) {
this.comms = comms;
}
public static class comms implements Serializable {
private String c,i;
public String getC() {
return c;
}
public void setC(String c) {
this.c = c;
}
public String getI() {
return i;
}
public void setI(String i) {
this.i = i;
}
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public String getCommentid() {
return commentid;
}
public void setCommentid(String commentid) {
this.commentid = commentid;
}
public String getCommentor() {
return commentor;
}
public void setCommentor(String commentor) {
this.commentor = commentor;
}
public String getCommentorid() {
return commentorid;
}
public void setCommentorid(String commentorid) {
this.commentorid = commentorid;
}
public String getDa() {
return da;
}
public void setDa(String da) {
this.da = da;
}
public String getFilename() {
return filename;
}
public void setFilename(String filename) {
this.filename = filename;
}
public String getMo() {
return mo;
}
public void setMo(String mo) {
this.mo = mo;
}
public String getStrip() {
return strip;
}
public void setStrip(String strip) {
this.strip = strip;
}
public String getStripname() {
return stripname;
}
public void setStripname(String stripname) {
this.stripname = stripname;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getYy() {
return yy;
}
public void setYy(String yy) {
this.yy = yy;
}
public String getMax() {
return max;
}
public void setMax(String max) {
this.max = max;
}
}
然后我通过插入这 4 个模拟条目在 mongodb 中创建了一些测试数据:
{ "_id" : ObjectId("518f14e5394594efbe18068c"), "body" : "1", "commentid" : "2525923", "commentor" : "Simon", "commentorid" : "769338", "da" : "25", "filename" : "/mnt/sshfs/gocomics/comments/100.out.bz2", "mo" : "11", "strip" : "luann", "stripname" : "Luann", "time" : "1 day ago", "yy" : "2011" }
{ "_id" : ObjectId("518f14e5394594efbe18068d"), "body" : "2", "commentid" : "2525924", "commentor" : "Josh", "commentorid" : "769339", "da" : "25", "filename" : "/mnt/sshfs/gocomics/comments/100.out.bz2", "mo" : "11", "strip" : "luann", "stripname" : "Luann", "time" : "1 day ago", "yy" : "2011" }
{ "_id" : ObjectId("518f14e5394594efbe18068e"), "body" : "3", "commentid" : "2525925", "commentor" : "Peter", "commentorid" : "769340", "da" : "25", "filename" : "/mnt/sshfs/gocomics/comments/100.out.bz2", "mo" : "11", "strip" : "luann", "stripname" : "Luann", "time" : "1 day ago", "yy" : "2011" }
{ "_id" : ObjectId("518f14e5394594efbe18068f"), "body" : "old1", "commentid" : "2525905", "commentor" : "Peter", "commentorid" : "769340", "da" : "24", "filename" : "/mnt/sshfs/gocomics/comments/100.out.bz2", "mo" : "11", "strip" : "luann", "stripname" : "Luann", "time" : "1 day ago", "yy" : "2011" }
然后我运行代码,结果如下:
[{"id":"luann-2011/11/25","max":"2525925","comms":{"c":"Simon","i":"2525923"}}]
结果可以解释为 postluann-2011/11/25
具有最大评论数(或 mongo id),2525925
而您的评论的 id 为2525923
. 因此,在您发表评论后会有稍后的评论,因此您需要获取该新评论。您将需要以编程方式为其编写逻辑。