Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb查找每个组的最新版本,并在Spring boot中实现_Mongodb_Spring Boot_Aggregation Framework_Mongotemplate - Fatal编程技术网

Mongodb查找每个组的最新版本,并在Spring boot中实现

Mongodb查找每个组的最新版本,并在Spring boot中实现,mongodb,spring-boot,aggregation-framework,mongotemplate,Mongodb,Spring Boot,Aggregation Framework,Mongotemplate,我是mongodb的新手。假设我有一个集合中的文档: [ { _id: ObjectId("1"), uid: "test", obj: Object name: "a", field1: "..." }, { _id: ObjectId("2"), uid: "t

我是mongodb的新手。假设我有一个集合中的文档:

  [
        {
          _id: ObjectId("1"),
          uid: "test",
          obj: Object
                    name: "a",
                    field1: "..."
        },

        {
          _id: ObjectId("2"),
          uid: "test",
          obj: Object
                    name: "a",
                    field1: "..."
        },
        {
          _id: ObjectId("3"),
          uid: "test",
          obj: Object
                    name: "b",
                    field1: "..."
        },
         {
          _id: ObjectId("4"),
          uid: "test2",
          obj: Object
                    name: "b",
                    field1: "..."
        }
    ]
我想检索具有唯一obj.name的obj列表,预期输出为:

  [
      {
         _id: ObjectId("2") // not really necessary , just to show that the obj was under ObjectId("2")
         name: "a",
         field1: "..."
      },
      {
         _id: ObjectId("3") // not really necessary , just to show that the obj was under ObjectId("3")
         name: "b",
         field1: "..."
      }
    ]
我对实现这一目标的想法是:

  • 首先匹配uid
  • 按_iddesc对结果排序
  • 按对象名称分组

    [{ $match:{ uid:“测试” } }, { $sort:{ _身份证:-1 } }, { $group:{ _id:“$obj.name” } }]

  • 我得到的是:

    {
      _id:"a"
    },
    {
      _id:"b"
    }
    
    第二个问题: 如何使用SpringBootMongo模板或其他SpringBootMongo库进行此类查询

     Public class A {
           private String _id;
           private String uid;
           private Obj obj;
        }
    
        Public class Obj {
               private String name;
               private String field1;
        }
    
    在Java意义上,我想检索
    List

    但是我不知道怎么做


    非常感谢您的帮助。

    非常简单。遵循规则

    添加以下代码:

    //Add this code to your service class
    @Autowired
    private MongoTemplate mongoTemplate;
    
    ...
    
    //Aggregation pipeline    
    Aggregation agg = Aggregation.newAggregation(
        Aggregation.match(Criteria.where("uid").is("test")),
        Aggregation.sort(Direction.DESC, "_id"),
        Aggregation.group("obj.name").first("obj").as("obj"),
        Aggregation.replaceRoot("obj")
    );
    
    AggregationResults<Obj> result = mongoTemplate.aggregate(agg, "collection", Obj.class).getMappedResults();
    //result.forEach(System.out::println);
    
    //将此代码添加到服务类
    @自动连线
    私有MongoTemplate MongoTemplate;
    ...
    //聚合管道
    聚合agg=Aggregation.newAggregation(
    Aggregation.match(标准,其中“uid”为“测试”),
    聚合.sort(Direction.DESC,“_id”),
    聚合集团(“obj.name”)。首先(“obj”)。作为(“obj”),
    聚合.replaceRoot(“obj”)
    );
    AggregationResults=mongoTemplate.aggregate(agg,“collection”,Obj.class).getMappedResults();
    //result.forEach(System.out::println);
    
    谢谢您的回答。我想问一下,如果我的obj类有多个字段,我是否需要使用。首先针对每个字段,还是有一种方法可以使用一个函数获取所有字段?@Aerondight您可以使用
    $first
    运算符获取整个
    obj
    。然后,您需要添加额外的
    Aggregation.replaceRoot()
    stage。再次检查我的答案。