On the one hand, the .getCollection() method returns the base object of the driver collection as follows:
DBCollection collection = mongoTemplate.getCollection("collectionName");
So the type of request object may be different from what you are using, but there are other things. Namely, that .distinct() returns only the "distint" values ββof the key that you requested, and do not return other fields in the document. So you can do:
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());
But this only returns the βpatternβ as a separate item in the list, for example.
If you want the fields to be different from the set, use the .aggregate() method .aggregate() . Using the "first" events of other field values ββfor an individual key:
DBCollection colllection = mongoTemplate.getCollection("collectionName"); List<DBObject> pipeline = Arrays.<DBObject>asList( new BasicDBObject("$match",new BasicDBObject("dataset","d1")), new BasicDBObject("$group", new BasicDBObject("_id","$source") .append("name",new BasicDBObject("$first","$name")) .append("description", new BasicDBObject("$first","$description")) ) ); AggregationOutput output = colllection.aggregate(pipeline);
Or the actual "excellent" values ββof several fields, making them part of the grouping key:
DBCollection colllection = mongoTemplate.getCollection("collectionName"); List<DBObject> pipeline = Arrays.<DBObject>asList( new BasicDBObject("$match",new BasicDBObject("dataset","d1")), new BasicDBObject("$group", new BasicDBObject("_id", new BasicDBObject("source","$source") .append("name","$name") .append("description","$description") ) ) ); AggregationOutput output = colllection.aggregate(pipeline);
There is also a direct .aggregate() method for .aggregate() instances, which has a number of helper methods for building pipelines. But that should point you in the right direction, at least.