4

I'm currently working on a project using Spring Data Mongo. My repository is just an interface extending MongoRepository. I would like to add a custom query method in order to retrieve all distinct values for one of my collection's fields.

I tried something like this:

@RepositoryRestResource(path = "devices", collectionResourceRel = "deviceInfos")
public interface DeviceInfoRepository extends MongoRepository<DeviceInfo, String> {

    @RestResource(path = "distinctUnitIds")
    List<String> findDistinctUnitIdBy();

}

With that code, Spring give me an error because it's not able to build my list. So I tried this:

@RepositoryRestResource(path = "devices", collectionResourceRel = "deviceInfos")
public interface DeviceInfoRepository extends MongoRepository<DeviceInfo, String> {

    @RestResource(path = "distinctUnitIds")
    List<DeviceInfo> findDistinctUnitIdBy();

}

That code works but the distinct seems to be totally ignored.

The documentation about Distinct in query method is really not clear...

Did I do something wrong? What's the best way to solve get the distinct values of a field using Spring Data?

Thanks!

Brice Argenson
  • 802
  • 2
  • 8
  • 13

2 Answers2

7

You will have to use Spring Data MongoTemplate - the MongoRepository interfaces are made only for basic functionality and for more fine grain control of what you are querying, its best to use MongoTemplate.

Here is an example of how one would get distinct values from a collection:

Criteria criteria = new Criteria();
criteria.where("dataset").is("d1");
Query query = new Query();
query.addCriteria(criteria);
List list = mongoTemplate.getCollection("collectionName")
    .distinct("source",query.getQueryObject());

Here is the link to more info: mongodb mongoTemplate get distinct field with some criteria

Community
  • 1
  • 1
Simon
  • 19,658
  • 27
  • 149
  • 217
  • 1
    Thanks for your answer. I was expecting something like that. Any idea of what the findDistinctBy described in the reference guide does? – Brice Argenson May 07 '16 at 01:07
  • The keyword distinct is not supported in mongo repositories, check the supported keywords here http://docs.spring.io/spring-data/data-document/docs/current/reference/html/#mongodb.repositories.queries – Sigrist May 08 '16 at 13:26
2

in SpringBoot2 you can do the following :

DistinctIterable<String> iterable = mongoTemplate.getCollection(COLLECTION_NAME).distinct("source",in(FieldValue,query.getQueryObject(), String.class);
        MongoCursor<String> cursor = iterable.iterator();
        List<String> list = new ArrayList<>();
        while (cursor.hasNext()) {
            list.add(cursor.next());
        }
        return list;
Karthikeyan
  • 2,634
  • 5
  • 30
  • 51
  • I am not clear with your answer. Could you please post complete solution? – PAA Apr 05 '19 at 12:29
  • Could you please guide here: https://stackoverflow.com/questions/55535331/spring-data-mongo-issue-with-distinct-collection ? – PAA Apr 05 '19 at 12:37
  • @PAA Both pretty same, except the above example uses an iterator to support scalability- which is advisable. Let me know if you still have any question – Karthikeyan Aug 21 '19 at 11:29