6

MongoDB version 3.6

I got the duplicate key message when I executed this. What's the actual problem?

mongos> db.setup.findAndModify({
... query : {"_id" : ObjectId("5b3c7abbea95705803084cad")},
... update : {$set : { "test" : "xxxx" }},
... upsert : true
... })
2018-07-06T10:13:22.749+0000 E QUERY    [thread1] Error: findAndModifyFailed failed: {
    "ok" : 0,
    "errmsg" : "E11000 duplicate key error collection: testdb.setup index: name dup key: { : null }",
    "code" : 11000,
    "codeName" : "DuplicateKey",
    "$clusterTime" : {
        "clusterTime" : Timestamp(1530872002, 603),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    },
    "operationTime" : Timestamp(1530872002, 602)
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DBCollection.prototype.findAndModify@src/mongo/shell/collection.js:724:1
@(shell):1:1
mongos>

3 Answers 3

9

This is the expected case if you have defined a unique index which is non-sparse: documents missing a value for the field with the unique index will have an indexed value of null. This means that only a single document in the collection can be missing the unique field.

The duplicate key message includes the index name and key violation:

"errmsg" : "E11000 duplicate key error collection: testdb.setup index: name dup key: { : null }",

In your example, the collection setup in database testdb has a unique index on the name field. The attempted upsert failed because the name field was missing and there was already a document in this collection with a missing or null value for name.

If you want to use a unique index without requiring the field to be present you have a few options:

  • Drop and recreate the unique index with the sparse property:

    db.setup.createIndex(
        {name: 1},
        {unique:true, sparse:true}
    )
    
  • Drop and recreate the unique index using a partial filter expression (which allows further criteria if needed):

    db.setup.createIndex(
       { name: 1},
       { unique:true, partialFilterExpression: {name: {$exists:true }}}
    )
    
4
  • Thanks, @Stennie, it's my bad to not see "name" field in error message.
    – tao hiko
    Commented Jul 10, 2018 at 4:24
  • Hi. This is not working for me. It still complains about duplicates in the unique field. Can "duplicate documents" contains the field {...,"name":null,...} or they just should not contain that field?
    – Miguel
    Commented Feb 25, 2019 at 11:39
  • @Miguel I suggest posting a new question with relevant details for your environment (MongoDB server version, driver version, error message, ...).
    – Stennie
    Commented Feb 25, 2019 at 17:54
  • @Stennie, I just posted the question here. Thanks
    – Miguel
    Commented Feb 26, 2019 at 14:39
2

Delete the collection from the DB and then hit he API , it will work. There is no mistake in code.

1
  • it would be great if you'd register your account and add some details around your answer. Maybe a pointer to the documentation that shows how to delete the collection or something.
    – Hannah Vernon
    Commented Dec 17, 2020 at 15:05
1

I found other indexes on that collection have been set "unique" to be "true" (exclude _id) then it shows error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.