No database can do this on top of my head (use a different column / field as input to limit uniqueness). To do this, you will need to do some data tuning. The easiest way is a unique constraint on the array field.
> db.foo.createIndex({ emails: 1 }, { unique: true } ) > db.foo.insert({ emails: [' example@gmail.com ', ' example222@gmail.com '] }) WriteResult({ "nInserted" : 1 }) > db.foo.insert({ emails: [' anotherone@gmail.com ', ' example@gmail.com '] }) WriteResult({ "nInserted" : 0, "writeError" : { "code" : 11000, "errmsg" : "E11000 duplicate key error index: test.foo.$emails_1 dup key: { : \" example@gmail.com \" }" } })
Now, depending on your application logic, this array of email addresses may even replace the original two fields. Or not. You decide. If not, you need to insert both the original fields and duplicate them in this array to verify uniqueness.
source share