Saving an existing document leads to insert when the identifier field is named `_id` without using `@Id`
See original GitHub issueHi! 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:
- Created 2 years ago
- Comments:8 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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:
@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 useorg.springframework.data.annotation.Id
._id
. When renaming that one toid
, 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 fromjavax.persistence.Id
Very appreciated for your patience!