Spring 如何编写Mongodb组聚合的条件查询

Spring 如何编写Mongodb组聚合的条件查询,spring,mongodb,hibernate,Spring,Mongodb,Hibernate,Mongodb$group: db.careLogBean.aggregate([{ $unwind: "$comments" }, { $sort: { "comments.time": -1 } }, { $group: { _id: "$_id", careGiverId: { $first: "$careGiverId" }, careGiverName

Mongodb
$group

db.careLogBean.aggregate([{
    $unwind: "$comments"
}, {
    $sort: {
        "comments.time": -1
    }
}, {
    $group: {
        _id: "$_id",
        careGiverId: {
            $first: "$careGiverId"
        },
        careGiverName: {
            $first: "$careGiverName"
        },
        comments: {
            $push: "$comments"
        }
    }
}])
在mongodb
$group
聚合内容中。。。如何使用java标准编写
查询语言…

您可以尝试以下聚合

import static org.springframework.data.domain.Sort.Direction.DESC;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.*;

Aggregation aggregation = newAggregation(
       unwind("comments"),
       sort(DESC, "comments.time"), 
       group("_id")
          .first("careGiverId").as("careGiverId")
          .first("careGiverName").as("careGiverName")
          .push("comments").as("comments"));

List<BasicDBObject> dbObjects = mongoOperations.aggregate(aggregation , collection_name, BasicDBObject.class).getMappedResults();
导入静态org.springframework.data.domain.Sort.Direction.DESC;
导入静态org.springframework.data.mongodb.core.aggregation.aggregation.*;
聚合=新聚合(
放松(“评论”),
排序(DESC,“comments.time”),
组(“\u id”)
.第一个(“医务人员ID”)。作为(“医务人员ID”)
.第一个(“医务人员姓名”)。作为(“医务人员姓名”)
.推送(“评论”)。作为(“评论”);
List dbObjects=mongoOperations.aggregate(聚合、集合名称、BasicDBObject.class).getMappedResults();

您可以根据您的设置调整上一条语句。

您可以显示到目前为止您已经尝试过的内容吗?很抱歉延迟重播veeram..DBObject group=new BasicDBObject(“$group”,new BasicDBObject(“\u id”,“$name”,“$salary”);没问题。您的
$group
帖子中的部分查询看起来不正确。你确定你有工作要求吗?是的…实际上我的要求在下面我正在检查…让你知道发生了什么欢迎你。您可以勾选答案中的勾选框。更多信息