question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Saving an existing document leads to insert when the identifier field is named `_id` without using `@Id`

See original GitHub issue

Hi! I’m using mongodb to handle some data. But something is wrong when using XX.save(Object object) to update an existed document. My entity is like

@Document(value = "test")
public class TestDO
{
    @Id
    @Field(value = "_id", targetType = FieldType.OBJECT_ID)
    private String _id;

    @Field(value = "pa")
    private Integer pa;

    @Field(value = "pb")
    private String pb;
}

And following is the DAO interface

public interface TestDAO extends MongoRepository<TestDO, String>
{
}

But When I use the following code and try to update the document, error appears

TestDO testDO = testDAO.findBy_idEquals(_id);
        testDO.setPc(new Date());
        testDO.setPa(98);
        testDO.setPb("66666");
        testDAO.save(testDO);

The error is E11000 duplicate key error collection: hospital.test index: _id_ dup key: { _id: ObjectId('6130d0e926df0b133148b57a') }; nested exception is com.mongodb.MongoWriteException: E11000 duplicate key error collection: hospital.test index: _id_ dup key: { _id: ObjectId('6130d0e926df0b133148b57a') }

seems save() can’t update the data. But I google that save() can do this. Can someone fix this? Would be appreciated.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
mp911decommented, Sep 6, 2021

Thanks for the reproducer. It’s quite a mess and it took a while to clean up the project to make it runnable. There are two issues that should be considered:

  1. The used @Id annotation comes from the JPA project (javax.persistence.Id). Since MongoDB isn’t related to JPA at all, the annotation is silently ignored. Please use org.springframework.data.annotation.Id.
  2. The identifier field is named _id. When renaming that one to id, then Spring Data MongoDB will identify that field as primary key and is able to correctly detect whether the entity is a new one or whether it should exist in the database.
0reactions
ppooii12009commented, Sep 6, 2021

Thanks for the reproducer. It’s quite a mess and it took a while to clean up the project to make it runnable. There are two issues that should be considered:

  1. The used @Id annotation comes from the JPA project (javax.persistence.Id). Since MongoDB isn’t related to JPA at all, the annotation is silently ignored. Please use org.springframework.data.annotation.Id.
  2. The identifier field is named _id. When renaming that one to id, then Spring Data MongoDB will identify that field as primary key and is able to correctly detect whether the entity is a new one or whether it should exist in the database.

Thanks! I have resolved this problem. Like what you said, I use the wrong @Id annotation from javax.persistence.Id Very appreciated for your patience!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Use an 'External ID' to set the values for audit fields
In order to use the External ID field to insert audit fields: You will need to use the Upsert option in Data Loader...
Read more >
How to add a Unique Identifier to a SharePoint list or library
If you are using a modern list, click +Add column > Show/hide columns · Click the checkbox next to the ID field, then...
Read more >
Keep identity values when bulk importing data - SQL Server
When you bulk import data that contains identity values to a SQL Server instance, by default, it assigns new values. You can choose...
Read more >
Working with Document Identifiers - RavenDB
Each document in a RavenDB database has a unique string associated with it, called an identifier. or a put document command, is assigned...
Read more >
Links in HTML documents - W3C
Syntax of anchor names; Nested links are illegal; Anchors with the id attribute; Unavailable and unidentifiable resources. Document relationships: the LINK ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found