Spring Data Mongo - Query methods and Distinct field
<p>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.</p>

<p>I tried something like this:</p>

<pre><code>@RepositoryRestResource(path = "devices", collectionResourceRel = "deviceInfos")
public interface DeviceInfoRepository extends MongoRepository&lt;DeviceInfo, String&gt; {

    @RestResource(path = "distinctUnitIds")
    List&lt;String&gt; findDistinctUnitIdBy();

}
</code></pre>

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

<pre><code>@RepositoryRestResource(path = "devices", collectionResourceRel = "deviceInfos")
public interface DeviceInfoRepository extends MongoRepository&lt;DeviceInfo, String&gt; {

    @RestResource(path = "distinctUnitIds")
    List&lt;DeviceInfo&gt; findDistinctUnitIdBy();

}
</code></pre>

<p>That code works but the distinct seems to be totally ignored.</p>

<p>The documentation about Distinct in query method is really not clear...</p>

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

<p>Thanks!</p>