[SERVER-16752] Unique key constraints not enforced after dropDatabase() Created: 07/Jan/15  Updated: 08/Jan/15  Resolved: 08/Jan/15

Status: Closed
Project: Core Server
Component/s: Index Maintenance
Affects Version/s: 2.6.6
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Dan Helyar Assignee: Ramon Fernandez Marina
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Operating System: ALL
Participants:

 Description   

I have a Node project with WebDriver test suite that runs 'mongo [database] --eval "db.dropDatabase();"' before each test run.

One of the tests attempts to create a duplicate record to test a

{unique : true}

constraint, however the record is saved no problem. The schema is defined by Mongoose.

The same test gives duplicate key error if I don't run dropDatabase() beforehand.



 Comments   
Comment by Ramon Fernandez Marina [ 08/Jan/15 ]

Yes, drop() and dropDatabase() will also remove any indexes you may have. I'm now closing this ticket, but please open new tickets if you run into other issues.

Regards,
Ramón.

Comment by Dan Helyar [ 08/Jan/15 ]

Apologies I've since learnt that I need to run ensureIndex or avoid dropDatabase() altogether otherwise the behaviour I'm experiencing should be expected.

Comment by Ramon Fernandez Marina [ 07/Jan/15 ]

I tried a simple reproducer on my end, but was unable to see any bugs:

> db.foo.drop()
false
> db.foo.insert({a:1})
WriteResult({ "nInserted" : 1 })
> db.foo.drop()
true
> db.foo.ensureIndex( { a: 1 }, { unique: true } )
{
        "createdCollectionAutomatically" : true,
        "numIndexesBefore" : 1,
        "numIndexesAfter" : 2,
        "ok" : 1
}
> db.foo.insert({a:1})
WriteResult({ "nInserted" : 1 })
> db.foo.insert({a:1})
WriteResult({
        "nInserted" : 0,
        "writeError" : {
                "code" : 11000,
                "errmsg" : "insertDocument :: caused by :: 11000 E11000 duplicate key error index: test.foo.$a_1  dup key: { : 1.0 }"
        }
})

Can you please share more details on how the test to check the {unique : true} constraint looks like?

Generated at Thu Feb 08 03:42:09 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.