'Distinct values ignoring a field in Spring Data Mongodb

In my spring boot app utilizing mongodb I am trying to get a list of distinct nested objects.

The document looks like this (other details removed fore brevity):

class Movie {
  otherproperties...;
  List<CastRoleLink> cast;
  ...
}

//structure of CastRoleLink
class CastRoleLink {
  String name;
  String urlName;
  String roleID;
}

//additional class for structure of results
class CastLink {
  String name;
  String urlName;
}

What I really want is a list of all the unique CastLink from all of the Movies matching my criteria, which means that I need all of the distinct CastRoleLink objects without the roleID. The tricky part is that for different movies I could have the same name and urlName properties but a different roleID property. In these cases I would consider them the same because the CastLink would be the same. My current solution is close but not quite right.

//query is Query object with criteria for Movies
mongoTemplate.findDistinct(query, "cast", Movies.class, CastLink.class);

This gives me duplicates when the name and urlName properties are the same but the roleID property is different. Is there a way that I can find distinct name and urlName objects while ignoring the roleID property?



Solution 1:[1]

I figured it out. The key is to use Aggregation.

Aggregation aggregation = newAggregation(
  match(criteria),
  unwind("cast"),
  group("cast.urlName").addToSet("cast.name").as("name").addToSet("cast.urlName").as("urlName")
  project("name").and("urlName").previousOpertaion(),
  sort(Sort.Direction.ASC, "name")
)

return mongoTemplate.aggregate(aggregation, Movie.class, CastLink.class).getMappedResults();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Greg