简体   繁体   English

不同查询但在 Spring MongoDB 上选择多个字段

[英]Distinct query but selecting multiple fields on Spring MongoDB

I have a User model on a Mongo collection that looks like this:我在 Mongo 集合中有一个User model,如下所示:

User.class用户.class

@Id
private String userId;
private String userName;
private String organizationId;
private String organizationName;
private String status

I need to get a list of OrganizationDTO to send as a response to the frontend.我需要获取OrganizationDTO列表以作为对前端的响应发送。 The DTO is shaped as below: DTO的形状如下:

OrganizationDTO.class组织机构DTO.class

private String organizationId;
private String organizationName;

But in the collection is obviously possible that different Users have the same organizationId and organizationName, so I want to list them only once.但在集合中显然可能不同的用户具有相同的 organizationId 和 organizationName,所以我只想列出它们一次。 The list of OrganizationDTO should contain every distinct organizationId/Name that has a status included in a set I choose. OrganizationDTO 列表应包含每个不同的 organizationId/Name,其状态包含在我选择的集合中。

I'll be glad to add everything that could be helpful if needed.如果需要,我很乐意添加所有可能有用的内容。

I tried using mongoTemplate.findDistinct() but it clearly isn't the solution I'm looking for.我尝试使用mongoTemplate.findDistinct()但它显然不是我正在寻找的解决方案。 The over-complicated "solution" I found is this:我发现的过于复杂的“解决方案”是这样的:

Query orgIdListQuery = new Query().addCriteria(
  Criteria.where("status").in(statusList)
);

List<String> organizationIdList = mongoTemplate.findDistinct(orgIdListQuery, "organizationId", User.class, String.class);

List<String> organizationNameList = mongoTemplate.findDistinct(orgIdListQuery, "organizationName", User.class, String.class);

// Map the two lists to get a single one with both fields, obtaining a list of OrganizationDTO

but I didn't like it at all, so I tried with aggregations:但我一点也不喜欢,所以我尝试了聚合:

MatchOperation match = new MatchOperation(getCriteria(statusList));  //getCriteria returns the Criteria as above
GroupOperation group = new GroupOperation(Fields.fields("organizationId", "organizationName"));
Aggregation aggregation = Aggregation.newAggregation(match, group);
AggregationResults<OrganizationDTO> results = mongoTemplate.aggregate(aggregation, User.class, OrganizationDTO.class);
return results.getMappedResults();

It seems that I'm near the right implementation, but at the moment the result is a List with some empty objects.似乎我接近正确的实现,但目前结果是一个包含一些空对象的列表。 Is this the right solution once the aggregation is correct?一旦聚合正确,这是正确的解决方案吗? Or maybe there's something better?或者也许有更好的东西?

I think the problem can be the result can't be serialized into your OrganizationDTO so you can try to add a $project stage into aggregation, something like this (not tested):我认为问题可能是结果无法序列化到您的OrganizationDTO中,因此您可以尝试将$project阶段添加到聚合中,如下所示(未测试):

ProjectionOperation project = Aggregation.project().andExclude("_id").and("organizationId").nested(Fields.fields("_id.organizationId")).and("organizationName").nested(Fields.fields("_id.organizationName"));
Aggregation aggregation = Aggregation.newAggregation(match, group, project);

And now the result is like the DTO and can be mapped so now should not be empty objects.现在结果就像 DTO,可以映射,所以现在不应该是空对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM