[SERVER-14128] Creating _id index with unique:false should result in error Created: 02/Jun/14  Updated: 11/Jul/16  Resolved: 15/Aug/14

Status: Closed
Project: Core Server
Component/s: Index Maintenance
Affects Version/s: 2.4.10, 2.6.1
Fix Version/s: 2.7.5

Type: Bug Priority: Major - P3
Reporter: Avi Levy Assignee: J Rassi
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to SERVER-8237 autoIndexId:false should be disabled Closed
related to SERVER-5335 Creation of _id index should automati... Backlog
is related to SERVER-14849 Creating descending _id index should ... Backlog
Operating System: ALL
Steps To Reproduce:

// Create a on unique index

use foo
db.createCollection("bar",

{capped: false, autoIndexId: false}

)
db.bar.getIndexes()
// Should return [ ]
db.bar.ensureIndex({_id:1},

{unique:false}

)
// index on _id will be returned
db.bar.getIndexes()
db.bar.insert(

{"_id" : 1}

)
// The insert here will fail
db.bar.insert(

{"_id" : 1}

)
db.dropDatabase()

// now the same thing, only the index is not on the id

use foo
db.createCollection("bar",

{capped: false, autoIndexId: false}

)
db.bar.ensureIndex(

{hash:1}

,

{unique:false}

)
// index on hash will be returned
db.bar.getIndexes()
db.bar.insert(

{"hash" : 1}

)
// The insert here will succeed
db.bar.insert(

{"hash" : 1}

)
db.dropDatabase()

// now the same thing without an index

use foo
db.createCollection("bar",

{capped: false, autoIndexId: false}

)
db.bar.insert(

{"_id" : 1}

)
// The insert here will succeed
db.bar.insert(

{"_id" : 1}

)
db.dropDatabase()

Participants:

 Description   

When creating an index on an the _id with unique:false, the uniqueness is still enforced on it.



 Comments   
Comment by Githook User [ 15/Aug/14 ]

Author:

{u'username': u'jrassi', u'name': u'Jason Rassi', u'email': u'rassi@10gen.com'}

Message: SERVER-14128 Disallow requests for non-unique _id indexes
Branch: master
https://github.com/mongodb/mongo/commit/a1042b421b4a1f046a0228ef316c95f001a4781b

Comment by J Rassi [ 05/Jun/14 ]

levyavi: only non-compound indexes on _id have the property of forced uniqueness.

Comment by Avi Levy [ 05/Jun/14 ]

Just notice that When doing db.createCollection("bar",

{capped: false, autoIndexId: false}

) no index is created.
I can later create a compound index with _id and some other field
db.bar.ensureIndex({_id:1, dummy:1},

{unique:false}

)
I can then insert duplicate ids.

Comment by J Rassi [ 02/Jun/14 ]

Updating title to "Creating _id index with unique:false should result in error".

Assigning to hari.khalsa@10gen.com for triage.

Comment by Andy Schwerin [ 02/Jun/14 ]

levyavi, replication depends on the uniqueness of the _id field, so I'm inclined to consider this a reporting bug. That is, the server should probably have returned an error to you when you attempted to create a non-unique index of _id. Having collections without unique indexes on _id is extremely risky outside the local database, both for correctness and performance of replication.

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