[SERVER-35988] MongoDB show duplicate key is null? Created: 06/Jul/18  Updated: 14/Aug/18  Resolved: 09/Jul/18

Status: Closed
Project: Core Server
Component/s: Querying
Affects Version/s: 3.6.2
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: JONGCHANA PHUWASATE Assignee: Stennie Steneker (Inactive)
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
Operating System: ALL
Steps To Reproduce:

Run this command

 

db.setup.findAndModify({
 query : {"_id" : ObjectId("5b3c7abbea95705803084cad")},
 update : {$set : { "test" : "xxxx" }},
 upsert : true  })

Participants:

 Description   

I got the dup key message when I executed this.

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>



 Comments   
Comment by JONGCHANA PHUWASATE [ 09/Jul/18 ]

Thank you for your information.

Comment by Stennie Steneker (Inactive) [ 09/Jul/18 ]

jongchana.phuwasate 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 this example, the collection setup in database testdb has a unique index called name. The attempted upsert failed because the name value 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 need to recreate the index with the sparse property or a partialFilterExpression with an $exists:true match on the unique field name.

Regards,
Stennie

Comment by JONGCHANA PHUWASATE [ 06/Jul/18 ]

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

Please add more information which index that duplicated for a guideline to investigate.

Generated at Thu Feb 08 04:41:41 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.