title: How to get list of all object ids with Spring Data MongoDB query generation
body: <p>How do I get a list of object IDs back from Spring Data MongoDB's automatic query generation?</p>
  <p>I'm expecting something like</p>
  <pre><code>public interface EntityRepo extends Repository&lt;Entity, ObjectId&gt; {
    List&lt;ObjectId&gt; findDistinctIdBy()
  }
  </code></pre>
  <p>But executing that query throws an exception that it can't parse the json. And the json returned is the full Entity object.</p>
  <p>I can already do this with a @Query annotation specifying a projection, or using the MongoTemplate distinct() or aggregate(). But I'm specifically looking for the automatic way to do this with their query generation syntax.</p>
  <p>According to <a href="https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#repositories.query-methods.query-creation" rel="nofollow noreferrer">Spring Data MongoDB Query Creation</a>, you can use the Distinct keyword. Maybe this is where my assumptions go wrong, but I'd expect that to return the distinct list of ObjectID or fields values, not the full documents. In mongosh db.coll.distinct() query will return the array of matching values for a given field. And you obviously have to specify the field. Something like db.coll.distinct('_id', {}, {}) will return</p>
  <pre><code>[ ObjectId(&quot;507f1f77bcf86cd799439011&quot;) ]
  </code></pre>
  <p>I just want that result back.</p>