title: Is there a way to avoid explicitly writing document fields as Strings in Spring Data MongoDB queries?
body: <p>I have recently started to use Spring Data MongoDB and I wonder if there is any way to avoid writing entities' attributes explicitly as they are stored in the database. For example, given the following class representing a MongoDB collection:</p>
  <pre><code>public class Employee {
  
    @Id
    public String id;
    
    private double salary;
  
    ...
  }
  </code></pre>
  <p>If I want to make a query using MongoTemplate like:</p>
  <pre><code>public List findEmployeeBySalaryRange(double salary) {
          Query query = new Query();
          query.addCriteria(Criteria.where(&quot;salary&quot;).lt(salary));
          ...
  }
  </code></pre>
  <p>I would like to avoid writing <code>&quot;salary&quot;</code>, since that will make the code harder to maintain in the future in case the field name changes. I am thinking of something like getting the field name from the class attribute, but I am not quite sure how. Is there a way to do it? I have looked into the documentation but did not find anything related unless I missed it.</p>
  <p>Thanks in advance.</p>