How do I get a list of object IDs back from Spring Data MongoDB's automatic query generation?
I'm expecting something like
public interface EntityRepo extends Repository<Entity, ObjectId> {
List<ObjectId> findDistinctIdBy()
}
But executing that query throws an exception that it can't parse the json. And the json returned is the full Entity object.
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.
According to Spring Data MongoDB Query Creation, 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
[ ObjectId("507f1f77bcf86cd799439011") ]
I just want that result back.