Indeks unik hanya perlu dibuat satu kali, setelah itu setiap penyisipan dokumen yang berisi alamat email duplikat akan gagal. Ini contohnya:
var server = MongoServer.Create("mongodb://localhost");
var db = server.GetDatabase("myapp");
var users = db.GetCollection<User>("users");
users.EnsureIndex(new IndexKeysBuilder()
.Ascending("EmailAddress"), IndexOptions.SetUnique(true));
var user1 = new User { EmailAddress = "[email protected]" };
var user2 = new User { EmailAddress = "[email protected]" };
try
{
users.Save(user1, WriteConcern.Acknowledged);
users.Save(user2, WriteConcern.Acknowledged); // <-- throws MongoSafeModeException
}
catch (MongoSafeModeException ex)
{
Console.WriteLine(ex.Message);
}