mongoose:在几个领域的独特性

这是我的模式:

var user = new Schema({ // other fields... email_1: String, email_2: String }); 

有没有办法确保email_1email_2的唯一性? 也就是说,如果某个电子邮件被保存为一个用户的email_1 ,则不能被其他人保存为email_1email_2

我已经尝试了复合索引,但它只检查电子邮件对,而且我需要所有电子邮件都是唯一的。

使用这样的事实,即数组被索引为多键索引 ,并将您的电子邮件存储为“有序对” [email1, email2]

 > db.emails.ensureIndex({ "emails" : 1 }, { "unique" : true }) > db.emails.insert({ "emails" : ["me@wdb.com", "metoo@wdb.com"] }) > db.emails.insert({ "emails" : ["you@eagor.com", "me@wdb.com"] }) // MongoDB says NO! > db.emails.insert({ "emails" : ["metoo@wdb.com", "myself@wdb.com"] }) // MongoDB says NO