1

I am using spring boot 1.5.1 and MongoDB version 3.4.6 .

I have a mongo document Hotel which has a list of Review .

The class has property userName.

@Document
public class Hotel {

    @Id
    private String id;
    private List<Review> reviews;

I want to search all the hotel by Review userName.

My HotelRepository has public List<Hotel> findByReviewsUserName(String userName);

When I am calling with user 'Salman' -

List<Hotel> list = this.hotelRepository.findByReviewsUserName(user);

this method returns result like below :

[
    {
        "id": "59b23c39c70ff63135f76b14",
        "name": "Signature",
        "reviews": [
            {
                "id": 1,
                "userName": "Salman",
                "rating": 8,
                "approved": true
            },
            {
                "id": 2,
                "userName": "Shahrukh",
                "rating": 5,
                "approved": false
            }
        ]
    }
]

What I want the reviews only of 'Salman' but it's also returning for others also.

What am I missing or how to do it?

What I have noticed is that if a single review user matched it returns the whole list of reviews which I don't want, I want reviews I have searched by name.

Mehraj Malik
  • 14,872
  • 15
  • 58
  • 85
  • 2
    @NeilLuun you closed the question for duplication but that question does not give an answer for `spring-boot`part of question. – barbakini Sep 08 '17 at 07:07
  • I was writing an aswer for you but the question is closed. You may consider to open a new question without the badge mongodb but only spring-data and spring-data-mongodb. By the way you are not using `spring-data-jpa` but `spring-data-mongodb` – barbakini Sep 08 '17 at 07:10
  • Do I need to ask question again?? cannot get it reopened?? – Mehraj Malik Sep 08 '17 at 07:11
  • 1
    I flagged the question for moderators but do't have enough score directly to open/close it like Neil Lunn. May be he sees comments and reopen it or moderators reopen it. – barbakini Sep 08 '17 at 07:13
  • Here is the new question https://stackoverflow.com/questions/46110743/how-to-get-only-matched-result-from-a-list-in-mongo – Mehraj Malik Sep 08 '17 at 07:17

0 Answers0