根据条件删除重复项

问题描述 投票:0回答:1

我下面的列表中有重复的元素

我想根据版本和日期属性从列表中删除所有重复项

这意味着,如果有重复的元素,我得到一个具有状态actif的元素,如果没有人具有状态actif,那么我得到具有最近日期的那个]

[
   {
      "_id" : ObjectId("5e1832df02f04352705457dd"),
      "product":"1",
      "version":{
         "state":"Actif",
         "name":"1.0.0"
      },
      "createdDate":"01/01/2020"
   },
   {
      "_id" : ObjectId("5e1832df02f04352705457ff"),
      "product":"1",
      "version":{
         "state":"A faire",
         "name":"3.0.0"
      },
      "createdDate":"01/01/2020"
   },
   {
      "_id" : ObjectId("5e1832df02f04352705457ee"),
      "product":"1",
      "version":{
         "state":"Archiver",
         "name":"2.0.0"
      },
      "createdDate":"02/01/2020"
   },
   {
      "_id" : ObjectId("5e1832df02f04352705457gg"),
      "product":"2",
      "version":null,
      "createdDate":"01/01/2020"
   },
   {
      "_id" : ObjectId("5e1832df02f04352705457yy"),
      "product":"2",
      "version":{
         "state":"Archiver",
         "name":"2.0.0"
      },
      "createdDate":"02/01/2020"
   },
   {
      "_id" : ObjectId("5e1832df02f04352705455ss"),
      "product":"3",
      "version":{
         "state":"Archiver",
         "name":"2.0.0"
      },
      "createdDate":"01/01/2020"
   }
]

输出应类似于:

[
   {
      "_id" : ObjectId("5e1832df02f04352705457dd"),
      "product":"1",
      "version":{
         "state":"Actif",
         "name":"1.0.0"
      },
      "createdDate":"01/01/2020"
   },
   {
      "_id" : ObjectId("5e1832df02f04352705457yy"),
      "product":"2",
      "version":{
         "state":"Archiver",
         "name":"2.0.0"
      },
      "createdDate":"02/01/2020"
   },
   {
      "_id" : ObjectId("5e1832df02f04352705455ss"),
      "product":"3",
      "version":{
         "state":"Archiver",
         "name":"2.0.0"
      },
      "createdDate":"01/01/2020"
   }
]

public List<Product> search() {
    final Query query = new Query().with(new Sort(new Order(Direction.DESC, "createdDate")));
    return mongoOperations.find(query, Product.class);
}

我们该怎么做?

java mongodb aggregation-framework spring-data-mongodb
1个回答
0
投票

您需要使用aggregate方法。

Java代码

未经测试!如果AggregationOperation希望实现toDocument方法,请将所有BasicDBObject更改为Document

import static org.springframework.data.mongodb.core.aggregation.Aggregation.*;

Aggregation aggregation = newAggregation(
        sort(Sort.Direction.ASC, "product", "version", "createdDate"), 
        group("product").last("$$ROOT").as("root"),
        sort(Sort.Direction.ASC, "_id"),
        project(Fields.from(Fields.field("_id", "$root._id"), 
          Fields.field("createdDate", "$root.createdDate"),
          Fields.field("product", "$root.product"), 
          Fields.field("version", "$root.version"))));

return mongoTemplate.aggregate(aggregation, Product.class, Product.class).getMappedResults();

MongoDB shell

db.Product.aggregate([
  {
    $sort: {
      product: 1,
      version: 1,
      createdDate: 1
    }
  },
  {
    $group: {
      _id: "$product",
      root: {
        $last: "$$ROOT"
      }
    }
  },
  {
    $sort: {
      _id: 1
    }
  },
  {
    $project: {
      _id: "$root._id",
      createdDate: "$root.createdDate",
      product: "$root.product",
      version: "$root.version"
    }
  }
])

MongoPlayground

© www.soinside.com 2019 - 2024. All rights reserved.