title: How to implement soft (logical) delete with MongoDB and Spring?
body: <p>I have Spring web app with MongoDB. Currently I always permanently delete data from database.</p>
  
  <pre><code>@Repository
  public class SessionRepository extends CrudRepository implements SessionService {
    ...
    @Override
    public void insert(Session session) {
      saveRoom(session);
      getTemplate().insert(session);
    }
  
    @Override
    public void delete(Session session) {
      getTemplate().remove(session);
    }
    ...    
  }
  </code></pre>
  
  <p>What would be a good way to change this into soft delete?</p>
  
  <p>----------------- edit 1 -------------------</p>
  
  <p>I understand the logic now what I should do, thanks Sarath Nair. But I am unsure how to implement this in Spring. I have a Session object:</p>
  
  <pre><code>@Document(collection = "session")
  public class Session {
  
    @Id
    private String id;
    private Date startDate;
    private Date endDate;
  //I just put this here
    private boolean deleted = false;
  
    public boolean isDeleted() {
      return deleted;
    }
  
    public void setDeleted(boolean deleted) {
      this.deleted = deleted;
    }
  
    ...
  }
  </code></pre>
  
  <p>I want the field <code>boolean isDeleted</code> to be present in the database but I don't wan't to send that piece of information out with a web service.
  <code>@Transient</code> is no good because then the field won't show up in database nor in the HTTP response. Right now I am sending <code>deleted: false</code> in my HTTP response.</p>
  
  <p>How should I edit my Session class?</p>