title: spring-data mongodb exclude fields from update
body: <p>How to make sure that specific fields can be inserted upon creation, but can optionally be excluded when updating the object.
  I'm essentially looking for something like the following:</p>
  
  <pre><code>mongoOperations.save(theObject, &lt;fields to ignore&gt;)
  </code></pre>
  
  <p>From what I see, recently introduced <strong>@ReadOnlyProperty</strong> will ignore the property both for inserts and updates.</p>
  
  <p>I was able to get the desired behavior by implementing my Custom MongoTemplate and overriding its <strong>doUpdate</strong> method as follows:</p>
  
  <pre><code>@Override
  protected WriteResult doUpdate(String collectionName, Query query,
          Update originalUpdate, Class&lt;?&gt; entityClass, boolean upsert, boolean multi) {
  
      Update updateViaSet = new Update();
  
      DBObject dbObject = originalUpdate.getUpdateObject();
      Update filteredUpdate = Update.fromDBObject(dbObject, "&lt;fields to ignore&gt;");
  
      for(String key : filteredUpdate.getUpdateObject().keySet()){
  
          Object val = filteredUpdate.getUpdateObject().get(key);
  
          System.out.println(key + "::" + val);
  
          updateViaSet.set(key, filteredUpdate.getUpdateObject().get(key));
      }
  
      return super
              .doUpdate(collectionName, query, updateViaSet, entityClass, upsert, multi);
  }
  </code></pre>
  
  <p>But the issue is that now it will use <a href="http://docs.mongodb.org/manual/reference/operator/update/set/#up._S_set" rel="noreferrer">Mongo $set</a> form of updates for everything, not just for specific cases.
  Please advise if there is any simpler (and correct) way to achieve this.</p>