1

ive organization id and email(optional) i want to create multidocument unique index for email, as email is optional iam not able to insert two documents with email as null value or a document without email.

ive listed the things which i ve tried

1.createIndex({ "organizationID":1, "email": 1 },{ sparse:true , unique: true});

2.createIndex({ "organizationID":1, "email": 1 },{ partialFilterExpression:{ email:{ $exist: true } }, unique: true});

3
  • create sparse index only for email field createIndex({ "email": 1 },{ sparse:true , unique: true}) Commented Jul 21, 2018 at 13:32
  • it should be unique based on organization id Commented Jul 21, 2018 at 13:39
  • unique sparse index works properly on sigle document, but the problem is with multi document sparse indexing Commented Jul 21, 2018 at 13:40

1 Answer 1

1

Just create a non-unique sparse index.

Since you can have multiple non-unique null values, it's impossible to mark this index as unique.

Sign up to request clarification or add additional context in comments.

1 Comment

createIndex({ "email": 1 },{ sparse:true , unique: true}) works

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.