1

I am trying to use nested Mongodb query but it does not work. It is similar to Spring data mongodb query for subdocument field

But suggestions mentioned there does not work.

Please find my documents below.

@Document
public class Ticket {

  @Id
  private String id;

  @DBRef
  @CascadeSave
  private Customer customer;

  // getters and setters   
}

@Document
public class Customer {

  @Id
  private String id;

  private String firstName;

  // getters and setters
}

public interface TicketRepository extends MongoRepository<Ticket, String> {

  public List<Ticket> findByCustomerFirstName(String firstName);

  }

I tried both findByCustomerFirstName and findByCustomer_FirstName but it does not work. Any suggestions ?

0

1 Answer 1

0

These suggestions are right it should work...

Official docs explains it as you did it:http://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#repositories.query-methods.query-property-expressions

Property expressions can refer only to a direct property of the managed entity, as shown in the preceding example. At query creation time you already make sure that the parsed property is a property of the managed domain class. However, you can also define constraints by traversing nested properties. Assume a Person has an Address with a ZipCode. In that case a method name of

List<Person> findByAddressZipCode(ZipCode zipCode);

creates the property traversal x.address.zipCode

Just one thing, remove @Document from Customer and try it, Mongodb didn't support join queries (I'm not sure if now it does)... so you're document should be Ticket and it must have a embbebed document Customer as a inner object and not in a different document.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much for your reply. I forgot to mention @DBRef annotation i used for customer. Reason is because i would like to access customer independent of ticket as well. Could u please suggest any alternative approach
Yes it works with inner documents :) but am looking for a way with linked documents –

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.