Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unique indexes treat missing value as "unique" #42

Closed
bencherian opened this issue Jan 10, 2019 · 2 comments
Closed

Unique indexes treat missing value as "unique" #42

bencherian opened this issue Jan 10, 2019 · 2 comments
Labels
compatibility Either missing or incompatible features causing driver or tool compatibility

Comments

@bencherian
Copy link

If you create an unique index and the fields of the index are missing, that will be treated as a unique value for the index. This is not how MongoDB behaves.

@apkar apkar added the compatibility Either missing or incompatible features causing driver or tool compatibility label Jan 10, 2019
@dongxinEric
Copy link
Contributor

As per the MongoDB document, unique index does NOT allow duplicated null fields:https://docs.mongodb.com/manual/core/index-unique/#unique-index-and-missing-field

I also tested locally with a fresh MongoDB instance:

> db.createCollection("tc")
{ "ok" : 1 }
> db.tc.insert({"a":1})
WriteResult({ "nInserted" : 1 })
> db.tc.insert({"a":2})
WriteResult({ "nInserted" : 1 })
> db.tc.find()
{ "_id" : ObjectId("5c37a995de954fb0637dec94"), "a" : 1 }
{ "_id" : ObjectId("5c37a997de954fb0637dec95"), "a" : 2 }
> db.tc.ensureIndex({"a":1}, unique=true)
{
	"createdCollectionAutomatically" : false,
	"numIndexesBefore" : 1,
	"numIndexesAfter" : 2,
	"ok" : 1
}
> db.tc.insert({"b":1})
WriteResult({ "nInserted" : 1 })
> db.tc.insert({"b":2})
WriteResult({
	"nInserted" : 0,
	"writeError" : {
		"code" : 11000,
		"errmsg" : "E11000 duplicate key error collection: test.tc index: a_1 dup key: { : null }"
	}
})

@bencherian
Copy link
Author

Sorry, I had misread the code I was testing on MongoDB.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compatibility Either missing or incompatible features causing driver or tool compatibility
Projects
None yet
Development

No branches or pull requests

3 participants