title: MongoDB repository save() do the insert instead of update
body: <p>I am writing a PUT request API with spring and mongodb. But the save() inserts a new object instead of update the current one.</p>
  <pre><code>@Document(&quot;Test&quot;)
  public class Expense {
  
      @Field(name = &quot;name&quot;)
      private String expenseName;
      @Field(name = &quot;category&quot;)
      private ExpenseCategory expenseCategory;
      @Field(name = &quot;amount&quot;)
      private BigDecimal expenseAmount;
  
      public Expense( String expenseName, ExpenseCategory expenseCategory, BigDecimal expenseAmount) {
          this.expenseName = expenseName;
          this.expenseCategory = expenseCategory;
          this.expenseAmount = expenseAmount;
      }
  
      public String getExpenseName() {
          return expenseName;
      }
  
      public void setExpenseName(String expenseName) {
          this.expenseName = expenseName;
      }
  
      public ExpenseCategory getExpenseCategory() {
          return expenseCategory;
      }
  
      public void setExpenseCategory(ExpenseCategory expenseCategory) {
          this.expenseCategory = expenseCategory;
      }
  
      public BigDecimal getExpenseAmount() {
          return expenseAmount;
      }
  
      public void setExpenseAmount(BigDecimal expenseAmount) {
          this.expenseAmount = expenseAmount;
      }
  }
  </code></pre>
  <p>This is my reporsitory class</p>
  <pre><code>public interface ExpenseRepository extends MongoRepository&lt;Expense, String&gt; {
  }
  </code></pre>
  <p>This is my Service class which shows how to update the class.</p>
  <pre><code>@Service
  public class ExpenseService {
      private final ExpenseRepository expenseRepository;
  
      public ExpenseService(ExpenseRepository expenseRepository) {
          this.expenseRepository = expenseRepository;
      }
  
      public void updateExpense(String id, Expense expense){
          Expense savedExpense = expenseRepository.findById(id)
                  .orElseThrow(() -&gt; new RuntimeException(
                          String.format(&quot;Cannot Find Expense by ID %s&quot;, id)));
  
          savedExpense.setExpenseName(expense.getExpenseName());
          savedExpense.setExpenseAmount(expense.getExpenseAmount());
          savedExpense.setExpenseCategory(expense.getExpenseCategory());
  
          expenseRepository.save(savedExpense);
      }
  }
  </code></pre>
  <p>This is my controller</p>
  <pre><code>@RestController
  @RequestMapping(&quot;/api/expense&quot;)
  public class ExpenseController {
  
      private final ExpenseService expenseService;
  
      public ExpenseController(ExpenseService expenseService) {
          this.expenseService = expenseService;
      }
      @PutMapping(&quot;/{id}&quot;)
      public ResponseEntity&lt;Object&gt; updateExpense(@PathVariable String id, @RequestBody Expense expense){
          expenseService.updateExpense(id, expense);
          return ResponseEntity.ok().build();
      }
  }
  </code></pre>
  <p>As shown in mongodb compass, mongodb auto generates an _id field for every object. So I do not define a id field or use @id annotation to define a primary for the collection. However, in the service class,  expenseRepository.findById(id) retrieves the desired object and update it. Why does save() do the insert instead of update? Many thanks.
  <a href="https://i.stack.imgur.com/vtItf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vtItf.png" alt="enter image description here" /></a></p>