title: Ignore fields from being passed on in Json Object
body: <p>I have a POJO called <code>User</code> which is also being used for inserting documents in MongoDb. </p>
  
  <pre><code>@Data
  @Document(collection = Constants.COLLECTION_USERS)
  public class User {
  
      public ObjectId _id;
  
      public String userID;
  
      public String email;
  
      public String name;
  
      public String sex;
  
      public String dob;
  
      public String photo;
      //have more variables
  
  }
  </code></pre>
  
  <p>I have a simple application where a user registers by giving in a subset of data listed in the <code>User</code> class. The signature of the register method in my controller is as follows.</p>
  
  <pre><code>public GenericResponse registerUser(@RequestBody User userRegistrationRequest)
  </code></pre>
  
  <p>It can be noticed that I am using the same POJO for the registration request. Until now everything is fine. I can persist the data user object just fine. </p>
  
  <p>This registration API is just used to persist a small set of a user's data. There would be other information as well in the MongoDb document, which would be accessed/persisted from some other APIs.</p>
  
  <p>Suppose a user has registered with the basic information and also has persisted other information via APIs other than the registration one.</p>
  
  <p>How would I make an API which can just get me some selective data from the <code>User</code> document again using the same User Pojo? If I call the repository to give data for a specific userID, it will give me the whole document mapped to the User class. I don't want my API to give all the information stored in the document.</p>
  
  <p><strong>One approach</strong> is to make another POJO with the details I want, and map the information selectively using a <code>Converter</code>. But, I want to avoid this approach, as I want to use the same class.</p>
  
  <p><strong>Second approach</strong>: Modify the Mongo query to return data selectively as given in the <a href="https://docs.mongodb.com/manual/tutorial/project-fields-from-query-results/" rel="nofollow noreferrer">docs</a>. But here I would have to specify all the fields I want in the result set. This would again be a length query.</p>
  
  <p>Is there a better way to filter out data from the object?</p>