title: Update updated_at attribute in mongo on each update using Morphia Java
body: <p>I want to update <code>update_at</code> attribute in MongoDB for each update on a document so later I can retrieve documents based on the last update_at filed. There are two ways I came across for this scenario.</p>
  
  <ol>
  <li><p>Update update_at in each DAO call like</p>
  
  <pre><code>public void updateMemberDetails(Long memberId, Sring email) {
  UpdateOperations&lt;Member&gt; ops =   getDs().createUpdateOperations(Member.class);
  Query&lt;Member&gt; query = basicQuery(memberId);
  ops.set("email", email);
  ops.set("update_at", new Date());
  getDs().update(query, ops);
  }
  </code></pre></li>
  <li><p>Other ways is to call Mongo's life cycle method in model (<a href="http://mongodb.github.io/morphia/1.4/guides/lifeCycleMethods/" rel="nofollow noreferrer">http://mongodb.github.io/morphia/1.4/guides/lifeCycleMethods/</a>)</p>
  
  <p><code>@PrePersist
    public void prePersist() {
      update_at = new Date();
    }</code></p></li>
  </ol>
  
  <p>Problem :- 
  Method 1 works perfectly fine but the only disadvantage of method 1 is I have to add <code>ops.set("update_at", new Date());</code> in each DAO update method that I don't want to update in each method.</p>
  
  <p>In method 2 <code>prePersist()</code> method called only in <code>operation db.save()</code>, prePersist() method is not called in db.update() operation. So I am not able to update date when DAO method call db.update().</p>