0

I want to write custom query on spring data mongodb. Something like this:

public interface CarRepo extends MongoRepository<Car, String> {


  @Query("select distinct(brand) from Car ")
  public List<String> findDistinctBrand();
}

But it's throwing an error "Caused by: com.mongodb.util.JSONParseException: ". How can I achieve that?

Mohit Tyagi
  • 2,788
  • 4
  • 17
  • 29
  • You are giving sql query in `@Query`part. this is completely wrong. And for distinct support of `mongodb`check [this](https://docs.mongodb.com/manual/reference/method/db.collection.distinct/) and check [this](https://stackoverflow.com/questions/19203724/distinct-in-spring-data-mongodb) question's answers for spirng data part – barbakini Oct 02 '17 at 09:24
  • But in criteria what should I write for the given query(select distinct(brand) from Car)? –  Oct 02 '17 at 09:32

3 Answers3

3

MongoDB does not support distinct command. It only supports returning distinct field values using the distinct command.

You need to use Mongodb template, for your results:

DBCollection colllection = mongoTemplate.getCollection("collectionName");
Criteria criteria = new Criteria();
criteria.where("your column").is("your value");
Query query = new Query();
query.addCriteria(criteria);
List list = mongoTemplate.getCollection("collectionName")
    .distinct("source",query.getQueryObject());
Yogi
  • 1,805
  • 13
  • 24
  • **this.mongoTemplate.getCollection("Car").distinct("brand")** This piece of code work for me . Thanks. –  Oct 02 '17 at 09:51
  • @yogi If the field source is dbref, in that case, it's not working. how can I get the dbref field also? – Nishant Bhardwaz Jan 13 '18 at 14:38
  • MongoTemplate is not required to query for distinct attributes, see: https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#repositories.query-methods.query-creation – Madbreaks Sep 19 '18 at 17:48
-1

You are using SQL to query in mongodb, but mongodb has its own query language. You need to write query in that language. As you are going to use distinct command you cannot user Query annotation or spring data query to do that. You need to create custom repository and execute distinct command using MongoTemplate.

Ruben Vardanyan
  • 1,298
  • 9
  • 19
  • Untrue, see: https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#repositories.query-methods.query-creation – Madbreaks Sep 19 '18 at 17:43
-1

Per the docs you should be able to accomplish this by simply defining the method in your repository interface, without the help of @Query:

public interface CarRepo extends MongoRepository<Car, String> {
    public List<String> findDistinctBrand();
}
Madbreaks
  • 19,094
  • 7
  • 58
  • 72
  • It is Throw Exception `Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property findDistinctPublisher found for type News!` – fuat May 05 '20 at 00:09
  • @fuat then you've done something else wrong. The docs clearly state this is doable, and have done so myself. – Madbreaks May 05 '20 at 04:55
  • I did this in repository interface `Set findDistinctPublisher();` `Publisher` is a property of the object. So is there anything wrong ? – fuat May 05 '20 at 13:04