title: How to use projecting types in Spring Data MongoDB repository's query methods?
body: <p>I have been playing around with Spring Data and MongoDB and have a question about limiting the amount of data for certain queries. I've seen adding custom queries within the <code>MongoRepository</code>, however I haven't seen any examples of limiting the amount of data and returning classes that are basically a subset of larger classes. </p>
  
  <p>For instance I have a <code>User</code> class that has several fields, but I also want to create a <code>UserShort</code> class that has a subset of the fields in the <code>User</code> class. For instance <code>UserShort</code> would only contain the <code>id</code> and <code>firstName</code> / <code>lastName</code> / <code>email</code> fields, rather than everything. </p>
  
  <p>I've seen I can specify/limit the fields that are returned, but can I have those returned into a different class? At the moment the <code>UserShort</code> will return <code>null</code> unless I specify the <code>User</code> class instead, but the fields will be limited to the ones I specify. Wasn't sure if this is possible at all? I realize the <code>User</code> class below isn't huge, but it's the concept I'm after.</p>
  
  <p>A user interface:</p>
  
  <pre><code>public interface Users {}
  </code></pre>
  
  <p>Subset class:</p>
  
  <pre><code>public class UserShort implements Users {
  
      @Id
      private String id;
  
      private String firstName;
      private String lastName;
  
      @Indexed(unique = true)
      private String email;
  
      //getters / setters
  }
  </code></pre>
  
  <p>Full class:</p>
  
  <pre><code>@Document
  public class User implements Users {
  
      @Id
      private String id;
  
      private String firstName;
      private String lastName;
      private String username;
      private String password;
      private Date dob;
      private Status status;
  
      @Indexed(unique = true)
      private String email;
  
      private Gender gender;
      private String locale;
      private Date registerDate;
  
      @DBRef
      private List&lt;UserAddress&gt; addresses;
  
      public User(){
          addresses = new ArrayList&lt;UserAddress&gt;();
      }
  
      //getters / setters
  }
  </code></pre>
  
  <p>And the repository interface:</p>
  
  <pre><code>public interface UserRepository extends MongoRepository&lt;Users, String&gt; {
  
      public User findByEmail(String email);
  
      @Query(value="{ 'email' : ?0 }", fields="{ 'firstName' : 1, 'lastName' : 1}")
      public UserShort findUserShortByEmail(String email);
  
  }
  </code></pre>