title: Spring Data MongoRepository save(T) not working... sometimes
body: <p>So there's this little Angular + Java + Spring Boot + MongoDB app I'm working with. It's getting quite a lot of action (read: code modifications) lately, but the data access classes have gone largely untouched AFAIK.<br>
  However, it seems like <code>MongoRepository</code> suddenly decided to stop persisting the changes I'm <code>save()</code>ing to DB.</p>
  
  <p>Inspecting <code>mongod.log</code> this is what I see when the <code>save()</code> works:</p>
  
  <pre><code>2018-04-11T15:04:06.840+0200 I COMMAND  [conn6] command pdfviewer.bookData command: find { find: "bookData", filter: { _id: "ID_1" }, limit: 1, singleBatch: true } planSummary: IDHACK keysExamined:1 docsExamined:1 idhack:1 cursorExhausted:1 keyUpdates:0 writeConflicts:0 numYields:1 nreturned:1 reslen:716 locks:{ Global: { acquireCount: { r: 4 } }, Database: { acquireCount: { r: 2 } }, Collection: { acquireCount: { r: 2 } } } protocol:op_query 102ms
  2018-04-11T17:30:19.615+0200 I WRITE    [conn7] update pdfviewer.bookData query: { _id: "ID_1" } update: { _class: "model.BookData", _id: "ID_1", config: { mode: "normal", offlineEnabled: true }, metadata: { title: "PDFdePrueba3pag   copia  6 ", ...}, downloaded: false, currentPageNumber: 2, availablePages: 3, bookmarks: [], stats: { _id: "c919e517-3c68-462c-8396-d4ba391762e6", dateOpen: new Date(1523460575872), dateClose: new Date(1523460575951), timeZone: "+2", ... }, ... } keysExamined:1 docsExamined:1 nMatched:1 nModified:1 keyUpdates:0 writeConflicts:1 numYields:1 locks:{ Global: { acquireCount: { r: 2, w: 2 } }, Database: { acquireCount: { w: 2 } }, Collection: { acquireCount: { w: 2 } } } 315ms
  2018-04-11T17:30:19.615+0200 I COMMAND  [conn7] command pdfviewer.$cmd command: update { update: "bookData", ordered: false, updates: [ { q: { _id: "ID_1" }, u: { _class: "model.BookData", _id: "ID_1", config: { mode: "normal", offlineEnabled: true }, metadata: { title: "PDFdePrueba3pag   copia  6 ", ...}, downloaded: false, currentPageNumber: 2, availablePages: 3, bookmarks: [], stats: { _id: "c919e517-3c68-462c-8396-d4ba391762e6", dateOpen: new Date(1523460575872), dateClose: new Date(1523460575951), timeZone: "+2", ... }, ... }, upsert: true } ] } keyUpdates:0 writeConflicts:0 numYields:0 reslen:55 locks:{ Global: { acquireCount: { r: 2, w: 2 } }, Database: { acquireCount: { w: 2 } }, Collection: { acquireCount: { w: 2 } } } protocol:op_query 316ms
  </code></pre>
  
  <p>And this is what I see when it doesn't:</p>
  
  <pre><code>2018-04-11T18:13:21.864+0200 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:64271 #1 (1 connection now open)
  2018-04-11T18:18:51.425+0200 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:64329 #2 (2 connections now open)
  2018-04-11T18:19:06.967+0200 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:64346 #3 (3 connections now open)
  </code></pre>
  
  <p>By doing a <code>tail -f</code><sup><sub>1</sub></sup> on the log file while debugging, I've seen those connections appear right when my code calls <code>findById()</code> or <code>save()</code>, so it seems like the app can reach the DB.  </p>
  
  <p>This is (more or less) the relevant Java code:</p>
  
  <pre><code>/* BookData.java */
  @Document
  public class BookData {
  
      @Id private String id;
      // Some more non-Id Strings...
      private Config config;
      private Metadata metadata;
      private Boolean downloaded;
      private Integer currentPageNumber;
      private int availablePages;
      private List&lt;Bookmark&gt; bookmarks;
      private StatsModel stats;
  
      @Transient private byte[] contents;
  
      public BookData() {}
  
      // getters and setters
  }
  
  /* BookDataRepository.java */
  // MongoRepository comes from spring-boot-starter-parent-1.4.5.RELEASE
  public interface BookDataRepository extends MongoRepository&lt;BookData, String&gt; {
      BookData findById(String id);
  }
  
  /* BookDataServiceImpl.java */
  public BookData updateBookData(String id, BookData newData) {
      final BookData original = bookDataRepository.findById(id);
      if (original == null) {
          return null;
      }
      original.setCurrentPageNumber(Optional.ofNullable(newData.getCurrentPageNumber()).orElseGet(original::getCurrentPageNumber));
      // similar code for a couple other fields
  
      return bookDataRepository.save(original);
  }
  </code></pre>
  
  <p>I've stepped through that part a hundred times while debugging and everything seems to be OK:  </p>
  
  <ul>
  <li><code>findById(id)</code> correctly returns the expected <code>BookData original</code> object: check ✓  </li>
  <li><code>newData</code> contains the expected values to be used for updating: check ✓  </li>
  <li>right before calling <code>save(original)</code>, <code>original</code> has been correctly modified using <code>newData</code> values: check ✓  </li>
  <li><code>save()</code> executes without errors: check ✓  </li>
  <li><code>save()</code> returns a new <code>BookData</code> with correctly updated values: to my own surprise, check ✓  </li>
  <li>after <code>save()</code> returns, a <code>db.bookData.find()</code> query in Mongo Shell shows that the values have been updated: <strong>fail</strong>.  </li>
  <li>after <code>save()</code> returns, the <code>BookData</code> object retrieved by new calls to <code>findById()</code> contains the updated values: <strong>fail</strong> (sometimes it does, sometimes it doesn't).  </li>
  </ul>
  
  <p>It just looks like MongoDB is waiting for some kind of <code>flush()</code>, but this is not a JPA repository where one could call <code>saveAndFlush()</code> instead.</p>
  
  <p>Any ideas why this might be happening?</p>
  
  <p>EDIT: versions (as requested):</p>
  
  <ul>
  <li>Java 8</li>
  <li>Spring Boot 1.4.5</li>
  <li>MongoDB 3.2.6</li>
  <li>Windows 10</li>
  </ul>
  
  <p>I also included <code>BookData</code> above.</p>