查找不同的嵌入式文档并使用 Field 进一步区分

Find Distinct Embedded Document and further make distinct using Field

我正在使用 Spring 启动 Mongo 示例。我经历了很多链接,如:I want result with distinct value of one field from mongodb using spring data,但仍然没有任何突破。我正在使用以下代码:

List<Object> obj = mongoTemplate.query(Health.class).distinct("healths").all();
List<Health> healths = null;
if (!CollectionUtils.isEmpty(obj)) {
    healths = obj.stream().map(e -> (Health) e).collect(Collectors.toList());
}

使用这段代码,我得到了重复的 HealthCode=E,如果我可以对 healthCd 字段做出决定,有什么办法吗?注意:healths 是 Patient 文档中的嵌入文档。

回复:

[
  {
    "healthCd": "D",
    "healthName": "ABC",
    "effDate": "2012-08-24T07:16:33"
  },
  {
    "healthCd": "C",
    "healthName": "MONO",
    "effDate": "2012-08-24T07:16:33"
  },
  {
    "healthCd": "E",
    "healthName": "BONO",
    "effDate": "2012-08-24T07:16:33"
  },
  {
    "healthCd": "B",
    "healthName": "JOJO",
    "effDate": "2012-08-24T07:16:33"
  },
  {
    "healthCd": "A",
    "healthName": "KOKO",
    "effDate": "2012-08-24T07:16:33"
  },
  {
    "healthCd": "1",
    "healthName": "LULU",
    "effDate": "2012-08-24T07:16:33"
  },
  {
    "healthCd": "E",
    "healthName": "BOBO",
    "effDate": "2014-07-26T22:37:49"
  }
]

健康

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Health {

    @Field
    private String healthCd;

    @Field
    private String healthName;

    @Field
    private LocalDateTime effDate;
}

您可以使用 MongoDB 聚合来获得所需的结果(取一个 look):

db.health.aggregate([
  {
    $sort: {
      "healths.effDate": 1
    }
  },
  {
    $group: {
      _id: "$healths.healthCd",
      healths: {
        $first: "$healths"
      }
    }
  },
  {
    $replaceRoot: {
      newRoot: "$healths"
    }
  }
])

MongoPlayground

Spring 引导实现

package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

@SpringBootApplication
public class DemoApplication implements CommandLineRunner {

    @Autowired
    private MongoTemplate mongoTemplate;

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {

//      //If your operator is not available inside Aggregation or query is too complex, 
//      //use below code to write MongoDB shell code directly as JSON
//      new AggregationOperation() {
//
//          @Override
//          public Document toDocument(AggregationOperationContext context) {
//              return new Document("$group", 
//                  new Document("_id", "$healths.healthCd")
//                      .append("healths", new Document("$first", "$healths")));
//          }
//          
//      },

        Aggregation agg = Aggregation.newAggregation(
            Aggregation.sort(Direction.ASC, "healths.effDate"),
            Aggregation.group("healths.healthCd").first("healths").as("healths"),           
            Aggregation.replaceRoot("healths")
        );

        AggregationResults<Healths> healths = mongoTemplate.aggregate(agg, 
                mongoTemplate.getCollectionName(Health.class), Healths.class);
        for (Healths health : healths.getMappedResults()) {
            Gson gson = new GsonBuilder().setPrettyPrinting().create();
            System.out.println(gson.toJson(health));
        }
    }
}