Spring MongoRepository is updating or upserting instead of inserting

Marc :

I'm using a :

org.springframework.data.mongodb.repository.MongoRepository

I start with an empty DB and create an object with _id = 1234 for example, and set some other String field to hello for example, and then do:

repository.save(object);

All is well, it saves the document in MondoDB.

I create a NEW object, set the same _id = 1234 but set the other String field to world and then to another save :

repository.save(newObject);

Results : the save works but updates the original object.

Expected results: This should fail with a DuplicateKeyException as _id is unique and I am using 2 separate objects when doing each save.

Defect in spring or am I doing something wrong ???

tinker :

Save, by definition, is supposed to update an object in the upsert style, update if present and insert if not. Read the save operation documentation on the MongoDb website

The insert operation in mongodb has the behavior you expect, but from the MongoRepository documentation it appears that insert is delegated to save so it won't make any difference. But you can give that a try and see if it works for you. Otherwise you can just do a get before to check if the object exists, since it is an index lookup it will be fast.

Edit: Check your repository version, insert was introduced in version 1.7.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

PHP is updating instead of inserting

Django is Inserting Record instead Of Updating

Springboot Jpa or hibernate updating records instead of inserting

Springboot Jpa updating records instead of inserting

Spring Data JPA inserting instead of Update

Coredata always updating the row instead of inserting a new row in swift

Entity Framework inserting new rows instead of updating them

Spring MongoRepository is Null

Autowire MongoRepository in Spring MVC

Is saveAll() of MongoRepository inserting data in one bulk?

Spring boot creates new row instead of updating

Java Spring : MongoRepository count() and findAll()

Spring Boot MongoRepository ignoring validation

Spring MongoRepository, where to catch the exception?

Resolve variable name for MongoRepository in Spring

MongoRepository JSON Date Query (Spring)

Morphia Upserting Field as int64 instead of in32

MongoRepository findOne using "id" instead of "_id"

Why does EF Core inserts before deleting instead of deleting and then inserting when updating a child list of a model?

on editing inserting a new row instead of updating the current row in cakephp 2x

Rails 5 is inserting instead of updating when changing the value of attribute in the join table

Updating and inserting, Duplicate keys

PDO inserting and updating

Inserting months and updating it with Amount

Spring app tries to recreate the DB on each run instead of updating

Spring JPA creates new record instead of updating existing record

Spring Data MongoRepository between for int values

could not found bean for MongoRepository (Spring Boot)

findByParentIdDeletedIsFalse like query in Spring Boot MongoRepository

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive