Upsert Mongo Document using spring data mongo
<p>I have a Class </p>

<pre><code>@Document
public class MyDocument {
   @Id
   private String id;
   private String title;
   private String description;
   private String tagLine;

   @CreatedDate
   private Date createdDate;

   @LastModifiedDate
   private Date updatedDate;

   public String getId() {
     return id;
   }
   public void setId(String id) {
     this.id = id;
   }

   public String getTitle() {
     return title;
   }

   public void setTitle(String title) {
     this.title = title;
   }

   public String getDescription() {
     return description;
   }

  public void setDescription(String description) {
    this.description = description;
  }
  public String getTagLine() {
     return tagLine;
   }

  public void setTagLine(String tagLine) {
    this.tagLine = tagLine;
  }
}
</code></pre>

<p>i have added annotated application with <code>@EnableMongoAuditing</code></p>

<p>i have created interface which implements mongorepository</p>

<p><code>public interface MyDocumentRepository extends MongoRepository&lt;MyDocument, String&gt; {
}
</code></p>

<p>when i have created RestController with <code>GET</code>,<code>POST</code>,<code>PATCH</code> methods
in <code>POST</code> I'm sending 
<code>{'title':'first'}
</code></p>

<p>Controller Class POST method is</p>

<pre><code>@RequestMapping(value = "/", method = RequestMethod.POST)
public ResponseEntity&lt;?&gt; saveMyDocument(@RequestBody MyDocument myDocument) { 
   MyDocument doc = myDocumentRepo.save(myDocument);
   return new ResponseEntity&lt;MyDocument&gt;(doc, HttpStatus.CREATED);
}
</code></pre>

<p>Its saving the data in mongo. </p>

<pre><code>{
    "_id" : ObjectId("56b3451f0364b03f3098f101"),
    "_class" : "com.wiziq.service.course.model.MyDocument",
    "title" : "test"
}
</code></pre>

<p>and PATCH request is like</p>

<pre><code>@RequestMapping(value = "/{id}", method = RequestMethod.PATCH)
public ResponseEntity&lt;MyDocument&gt; updateCourse(@PathVariable(value = "id") String id,
        @RequestBody MyDocument myDocument) {
    myDocument.setId(id);
    MyDocument doc = courseService.save(myDocument);
    return ResponseEntity.ok(course);
}
</code></pre>

<p>when in make PATCH request with data <code>{"description":"This is test"}</code>
it update the docuent <strong>BUT</strong> it removes title field and createdDate form the document, its doing update which is ok. But i wanted to do an upsert, i can do its using mongoTemplate, 
but there i have to set each property which i want to set.</p>

<p>Is there any generic way to that if i get a PATCH request i can update only not null properties.. properties which are coming in request</p>

<p>spring-data-rest seems to do it using @RepositoryRestResource. How can i achieve the same.</p>

<p>I don't want to code like this
<code>Update update = new Update().set("title", myDocument.getTitle()).set("description", myDocument.getdescription());</code></p>