从Mongo Collection符合某些条件的Mongo Collection中检索重复文档
以下是一个名为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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用MongoDB语法,您可以这样做:
由于您需要结果中的原始文档,因此需要在
$ $ group
步骤中保留它们。一种方法是$ push:“ $ root”
。最后两个步骤只是将其按照您的意愿进行格式化,因为它们被分组在一起。
查看其在
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.
See how it works on the playground example