I am switching from Postgres to MongoDB in my Spring Boot application. I enjoyed the privilege of being able to query my database using native queries via the @Query
annotation:
@Query(value = "SELECT id\n" +
"FROM ( \n" +
"SELECT DISTINCT CASE sender WHEN 1 THEN recipient ELSE sender END AS friend_id \n" +
"FROM friendrequest WHERE 1 IN (recipient, sender) \n" +
"AND friendrequeststatus = 'ACCEPTED' \n" +
") f \n" +
"LEFT JOIN users u\n" +
"ON f.friend_id = u.id\n" +
"ORDER BY friend_id \n-- #pageable\n"
,nativeQuery = true)
Page<User> getFriends(Pageable pageable);
Presently I am switching to MongoDB, and Inam using the same models. Is it possible to write advanced native queries using the @Query
method, or am I constrained to using the Query
object:
user = mongoTemplate.findOne(
Query.query(Criteria.where("name").is("Jack")), User.class);
I asked this question because I haven't seen any articles on building complex queries with MongoDB and Spring Boot.