I need to write an application with which I can do complex queries using spring-data and MongoDB. I started using the MongoRepository but struggled with complex queries to find examples or understand the Syntax.
I'm talking about queries like this:
@Repository
public interface UserRepositoryInterface extends MongoRepository<User, String> {
List<User> findByEmailOrLastName(String email, String lastName);
}
or the use of JSON based queries which I tried by trial and error because I didn't get the syntax right. Even after reading the MongoDB documentation (non-working example due to the wrong syntax).
@Repository
public interface UserRepositoryInterface extends MongoRepository<User, String> {
@Query("'$or':[{'firstName':{'$regex':?0,'$options':'i'}},{'lastName':{'$regex':?0,'$options':'i'}}]")
List<User> findByEmailOrFirstnameOrLastnameLike(String searchText);
}
After reading all the documentation it seems that mongoTemplate
is far better documented than MongoRepository
. I'm referring to the following documentation:
http://static.springsource.org/spring-data/data-mongodb/docs/current/reference/html/
Can you tell me which is more convenient and powerful to use? mongoTemplate
or MongoRepository
? Are both same mature or does one lack more features than the other?