从Mongo Collection符合某些条件的Mongo Collection中检索重复文档

发布于 2025-02-10 10:13:47 字数 1677 浏览 1 评论 0原文

以下是一个名为CustomerFinance的Mongo集合,其中包含4个文档的列表,

[
    {"VIN": 100,
    "CUSTOMER_TYPE": "1-PRIMARY BORROWER",
    "CUSTOMER_FIRST_NAME":"MATT",
    "DEAL_TYPE":"LEASE",
    "CONTRACT_START_DATE": "04/30/2021"},
    {"VIN": 101,
    "CUSTOMER_TYPE": "1-PRIMARY BORROWER",
    "CUSTOMER_FIRST_NAME":"JOHN",
    "DEAL_TYPE":"LEASE",
    "CONTRACT_START_DATE": "04/30/2021"},
    {"VIN": 101,
    "CUSTOMER_TYPE": "6-CO BORROWER",
    "CUSTOMER_FIRST_NAME":"SUSAN",
    "DEAL_TYPE":"LEASE",
    "CONTRACT_START_DATE": "04/30/2022"},
    {"VIN": 100,
    "CUSTOMER_TYPE": "1-PRIMARY BORROWER",
    "CUSTOMER_FIRST_NAME":"MATT",
    "DEAL_TYPE":"RETAIL",
    "CONTRACT_START_DATE": "04/30/2022"}
]

我的目标是仅从此集合中检索第一和第四个文档,因为您可以看到它们都具有以下字段相同的字段 - vin,customer_type,customer_first_name

具有以下java的件代码,我能够过滤那些大于1的VIN

GroupOperation groupByVin = group("VIN").count().as("VIN_COUNT");
MatchOperation filterVins = match(new Criteria("VIN_COUNT").gt(1));
Aggregation aggregation = newAggregation(groupByVin, filterVins);
AggregationResults<Document> result = mongoTemplate.aggregate(aggregation, "customerFinance", Document.class);
return result.getMappedResults();

,而我得到以下结果,

   [{"_id": 100,
    "VIN_COUNT": 2}]

But I want my result to look like the below format

[
{"VIN": 100,
 "CUSTOMER_TYPE": "1-PRIMARY BORROWER",
 "CUSTOMER_FIRST_NAME":"MATT",
 "DEAL_TYPE":"LEASE",
 "CONTRACT_START_DATE": "04/30/2021"
},
{"VIN": 100,
 "CUSTOMER_TYPE": "1-PRIMARY BORROWER",
 "CUSTOMER_FIRST_NAME":"MATT",
 "DEAL_TYPE":"RETAIL",
 "CONTRACT_START_DATE": "04/30/2022"
}
]

请您帮助我了解如何实现这一目标?

Below is a mongo collection named customerFinance containing a list of 4 documents

[
    {"VIN": 100,
    "CUSTOMER_TYPE": "1-PRIMARY BORROWER",
    "CUSTOMER_FIRST_NAME":"MATT",
    "DEAL_TYPE":"LEASE",
    "CONTRACT_START_DATE": "04/30/2021"},
    {"VIN": 101,
    "CUSTOMER_TYPE": "1-PRIMARY BORROWER",
    "CUSTOMER_FIRST_NAME":"JOHN",
    "DEAL_TYPE":"LEASE",
    "CONTRACT_START_DATE": "04/30/2021"},
    {"VIN": 101,
    "CUSTOMER_TYPE": "6-CO BORROWER",
    "CUSTOMER_FIRST_NAME":"SUSAN",
    "DEAL_TYPE":"LEASE",
    "CONTRACT_START_DATE": "04/30/2022"},
    {"VIN": 100,
    "CUSTOMER_TYPE": "1-PRIMARY BORROWER",
    "CUSTOMER_FIRST_NAME":"MATT",
    "DEAL_TYPE":"RETAIL",
    "CONTRACT_START_DATE": "04/30/2022"}
]

My goal is to retrieve only the first and fourth document from this collection as you can see they both have the following fields same - VIN, CUSTOMER_TYPE, CUSTOMER_FIRST_NAME

With the below piece of Java code, I am able to filter those VINs whose count is greater than 1

GroupOperation groupByVin = group("VIN").count().as("VIN_COUNT");
MatchOperation filterVins = match(new Criteria("VIN_COUNT").gt(1));
Aggregation aggregation = newAggregation(groupByVin, filterVins);
AggregationResults<Document> result = mongoTemplate.aggregate(aggregation, "customerFinance", Document.class);
return result.getMappedResults();

And I get the below result

   [{"_id": 100,
    "VIN_COUNT": 2}]

But I want my result to look like the below format

[
{"VIN": 100,
 "CUSTOMER_TYPE": "1-PRIMARY BORROWER",
 "CUSTOMER_FIRST_NAME":"MATT",
 "DEAL_TYPE":"LEASE",
 "CONTRACT_START_DATE": "04/30/2021"
},
{"VIN": 100,
 "CUSTOMER_TYPE": "1-PRIMARY BORROWER",
 "CUSTOMER_FIRST_NAME":"MATT",
 "DEAL_TYPE":"RETAIL",
 "CONTRACT_START_DATE": "04/30/2022"
}
]

Could you please help me understand on how can I achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

再浓的妆也掩不了殇 2025-02-17 10:13:47

使用MongoDB语法,您可以这样做:

由于您需要结果中的原始文档,因此需要在$ $ group步骤中保留它们。一种方法是$ push:“ $ root”
最后两个步骤只是将其按照您的意愿进行格式化,因为它们被分组在一起。

db.collection.aggregate([
  {$group: {_id: "$VIN", data: {$push: "$ROOT"}, count: {$sum: 1}}},
  {$match: {count: {$gt: 1}}},
  {$unwind: "$data"},
  {$replaceRoot: {newRoot: "$data"}}
])

查看其在

With mongoDB syntax you can do it like this:

Since you want the original documents in your results, you need to keep them during the $group step. One way of doing it, is $push: "$ROOT".
The last two steps are just to format it as you wanted, since they are grouped together.

db.collection.aggregate([
  {$group: {_id: "$VIN", data: {$push: "$ROOT"}, count: {$sum: 1}}},
  {$match: {count: {$gt: 1}}},
  {$unwind: "$data"},
  {$replaceRoot: {newRoot: "$data"}}
])

See how it works on the playground example

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文