title: How to Get Only Matched Result from An Array Field of Document in Mongo Using Spring Data
body: <p>I am using <code>spring boot 1.5.1</code> and <code>MongoDB version 3.4.6</code> .</p>
  
  <p>I have a mongo document Hotel which has a list of Review .</p>
  
  <p>The <code>Review</code> class has property <code>userName</code>.</p>
  
  <pre><code>@Document
  public class Hotel {
  
      @Id
      private String id;
      private List&lt;Review&gt; reviews;
  </code></pre>
  
  <p>I want to search all the hotel by Review userName.</p>
  
  <p>My <code>HotelRepository</code> has <code>public List&lt;Hotel&gt; findByReviewsUserName(String userName);</code></p>
  
  <p>When I am calling with user 'Salman' -</p>
  
  <pre><code>List&lt;Hotel&gt; list = this.hotelRepository.findByReviewsUserName(user);
  </code></pre>
  
  <p>this method returns result like below :</p>
  
  <pre><code>[
      {
          "id": "59b23c39c70ff63135f76b14",
          "name": "Signature",
          "reviews": [
              {
                  "id": 1,
                  "userName": "Salman",
                  "rating": 8,
                  "approved": true
              },
              {
                  "id": 2,
                  "userName": "Shahrukh",
                  "rating": 5,
                  "approved": false
              }
          ]
      }
  ]
  </code></pre>
  
  <p>What I want the reviews only of 'Salman' but it's also returning for others also.</p>
  
  <p>What am I missing or how to do it?</p>
  
  <p><strong>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.</strong></p>