I have a Springboot app which uses contains 2 Entities, User and Friend Request. The user Entity looks like this:
public class User{
@Id
private BigInteger id;
private String firstName;
private String lastName;
private String email;
private String password;
}
while the FriendRequest Object looks like this:
public class FriendRequest{
@Id
private BigInteger id;
private User sender;
private FriendRequestStatus friendRequestStatus;
private User recipient;
}
The friend request class has 4 properties:
The ID
the sender of a request which is a user
the recipient of a request which is also user
The friendRequestStatus - which can be Pending (if the request has not been accepted), Accepted (if the friend request has been accepted), or Declined (if the request has been declined).
instead of me making a friends table, i want to use the FriendRequest table to get all the friends of a particular user - it works by getting all friends request where a Specific User is a send or receiver and friendRequest is Accepted. Then going further to select the Sender as a friend if the recipient is the user whos friends you are looking for and selecting the recipient if the Sender was the user whos friends you are looking for. At the end i want to get a List.
My problem is i dont know how to achieve this in MongoDB for springboot , when using Postgre this was the query i ran, and it worked perfectly:
@Query(value = "SELECT * \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);
but how to replicate it in MongoDB for Springboot is a big problem