How to get actual value for duplicate index MongoDB?

I am interacting with MongoDB using C# driver. It threw duplicate key error like below for one index

the highlighted value 0x141214141a24 is not a actual value in the error message of location.path. how to get the actual value of it? (location.target is actual value I see)

Message : “E11000 duplicate key error collection: Database.Collection index: location.path_1_location.target_1 collation: { locale: “en_US”, caseLevel: false, caseFirst: “off”, strength: 1, numericOrdering: false, alternate: “non-ignorable”, maxVariable: “punct”, normalization: false, backwards: false, version: “57.1” } dup key: { location.path: “0x141214141a24”, location.target: 457 }” } ].

Hi Souvik,

Assuming that value you’ve noted doesn’t exist, would you mind advising how many documents have the location.target value of 457?

Although the value of the field location.path looks like a hex value, from the error message I’m assuming it’s stored as a string type.

Have you tried catching this duplicate key error in your code and handling it by e.g. printing the document on the logs when it happened?

Lastly, can you provide the following information:

  1. The command which generated this error.
  2. The MongoDB version in use.
  3. Some example documents
  4. The unique index definition of the collection

Regards,
Jason

1 Like

Thank you @Jason_Tran for your response.

would you mind advising how many documents have the location.target value of 457 ? - we have 868144 documents.

Have you tried catching this duplicate key error in your code and handling it by e.g. printing the document on the logs when it happened? – I have not handled from code. it was thrown from C# mongo driver.

  1. The command which generated this error. - await collection.BulkWriteAsync(write);

  2. The MongoDB version in use. - 6.0.8

    1. The unique index definition of the collection -
{
    "target" : 1,
    "path" : 1
}
  1. ex -
{
    "_id" : "635448f52be262cf567a6",
    "links" : null,
    "location" : [ 
        {
            "path" : 123,
            "target" : "67555276",
        }
    ],
    "path" : 123,
    "entityid" : null
}