• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: MongoDB says “duplicate key” for index field but it isn’t

Resolved: MongoDB says “duplicate key” for index field but it isn’t

0
By Isaac Tonny on 17/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

So I have a simple collection with an additional index on one field called “reactionId”. I then want to bulk-upsert multiple documents to that collection. The first one gets inserted, and then I get the error
I don’t see why the DB is reporting a duplicate key when they clearly are different? MongoDB version is 4.0.26
This is the Reaction model:
The insert operation:
And the index is defined like this:

Answer:

The problem is that the index is defined on the property “reactionID” – notice the upper-case ID. In your model, the property is named “reactionId” with a lower-case d. Hence, when inserting, your document does not contain a value for the property that MongoDB expects. This leads to a null value for both documents in the index – hence the duplicate key violation.
If you drop the index and redefine it for “reactionId”, this insert should work.

If you have better answer, please add a comment about this, thank you!

c#
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: How can I write CSS selector(s) that apply to table rows for all td elements on that row after a td with a certain class?

01/04/2023

Resolved: How do I use SetWindowText with Unicode in Win32 using PowerShell?

01/04/2023

Resolved: Shopware 400 Status Error “This value is too long. It should have 255 character or less.” When I Try Updating Database Table

01/04/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.