使用C#创建MongoDB唯一键

时间:2011-06-02 18:51:08

标签: c# mongodb mongodb-.net-driver

我与MongoDB共计n00b,我正在努力创建一个独特的字段EmailAddress。我已经在论坛中看到我必须创建一个索引,但到目前为止它对我来说并没有用。有人有代码示例吗?我是否必须在每次保存/调用时创建索引,还是只创建一次?

我试过这段代码:

DB.GetCollection<User>(Dbname)
    .EnsureIndex(new IndexKeysBuilder()
        .Ascending("EmailAddress"), IndexOptions.SetUnique(true));

DB.GetCollection<User>(Dbname).Save(user, SafeMode.True);

我的User模型如下所示:

public class User
{
    [Required(ErrorMessage = "Email Required")]
    public string EmailAddress { get; set; }

    public ObjectId Id { get; set; }

    public string FirstName { get; set; }
    public string LastName { get; set; }
}

7 个答案:

答案 0 :(得分:27)

只需创建一次唯一索引,之后包含重复电子邮件地址的任何文档插入都将失败。这是一个例子:

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 = "joe@example.com" };
var user2 = new User { EmailAddress = "joe@example.com" };

try
{
    users.Save(user1, WriteConcern.Acknowledged);
    users.Save(user2, WriteConcern.Acknowledged);  // <-- throws MongoSafeModeException
}
catch (MongoSafeModeException ex)
{
    Console.WriteLine(ex.Message);
}

答案 1 :(得分:20)

根据C#mongo驱动程序2.0版规范,不推荐使用EnsureIndex():http://api.mongodb.org/csharp/current/html/M_MongoDB_Driver_MongoCollection_EnsureIndex_2.htm

继承人如何通过2.0代码实现异步:

   var options = new CreateIndexOptions() { Unique = true };
   IndexKeysDefinition<Foo> keyCode = "{ Code: 1 }";
   var codeIndexModel = new CreateIndexModel<Foo>(keyCode, options);

如果是非字符串索引:

a = 24
b = 1.2

def test(x, y):
    return x + y

答案 2 :(得分:8)

您的代码看起来正确。这是一个完整的运行程序供您比较:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Builders;

namespace TestEnsureIndexOnEmailAddress {
    public class User {
        public ObjectId Id;
        public string FirstName;
        public string LastName;
        public string EmailAddress;
    }

    public static class Program {
        public static void Main(string[] args) {
            var server = MongoServer.Create("mongodb://localhost/?safe=true");
            var database = server["test"];
            var users = database.GetCollection<User>("users");
            if (users.Exists()) { users.Drop(); }

            users.EnsureIndex(IndexKeys.Ascending("EmailAddress"), IndexOptions.SetUnique(true));
            var john = new User { FirstName = "John", LastName = "Smith", EmailAddress = "jsmith@xyz.com" };
            users.Insert(john);
            var joe = new User { FirstName = "Joe", LastName = "Smith", EmailAddress = "jsmith@xyz.com" };
            users.Insert(joe); // should throw exception
        }
    }
}

您还可以使用mongo shell确认索引已创建:

> db.users.getIndexes()
[
        {
                "name" : "_id_",
                "ns" : "test.users",
                "key" : {
                        "_id" : 1
                },
                "v" : 0
        },
        {
                "_id" : ObjectId("4de8152ee447ad2550e3b5fd"),
                "name" : "EmailAddress_1",
                "ns" : "test.users",
                "key" : {
                        "EmailAddress" : 1
                },
                "unique" : true,
                "v" : 0
        }
]
>

答案 3 :(得分:0)

下面的2.8版本是创建索引的方法。请注意最后两行。 CreateOneAsync(indexDefinition,options)已过时。

var mongoClient = new MongoClient("connection");
var db = mongoClient.GetDatabase("database");

var options = new CreateIndexOptions() { Unique = true };
var field = new StringFieldDefinition<User>("EmailAddress");
var indexDefinition = new IndexKeysDefinitionBuilder<User>().Ascending(field);

var indexModel = new CreateIndexModel<User>(indexDefinition,options);
await db.GetCollection<Users>("users").Indexes.CreateOneAsync(indexModel);

答案 4 :(得分:0)

这是一个使用MongoDB.Entities

的工作程序
using System;
using MongoDB.Driver;
using MongoDB.Entities;

namespace StackOverflow
{
    public class User : Entity
    {
        public string Name { get; set; }
        public string EmailAddress { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            new DB("test");

            DB.Index<User>()
              .Options(o => o.Unique = true, o => o.Background = false)
              .Key(u => u.EmailAddress, Type.Ascending)
              .Create();

            var user1 = new User { Name = "First User", EmailAddress = "email@domain.com" };
            user1.Save();

            try
            {
                var user2 = new User { Name = "Second User", EmailAddress = "email@domain.com" };
                user2.Save();
            }
            catch (MongoWriteException x)
            {
                Console.WriteLine(x.Message);
                Console.ReadKey();
            }
        }
    }
}

尝试创建具有相同电子邮件地址的第二个用户会导致以下异常:

A write operation resulted in an error. E11000 duplicate key error collection: test.User index: EmailAddress(Asc) dup key: { : "email@domain.com" }

答案 5 :(得分:0)

在我看来,一种更好,更可行的方法是创建一个自定义属性,然后使用c#,您可以使用该属性装饰属性并添加一个唯一索引,这种方法将通用并且使用起来非常简单。下面的示例:

我的自定义属性: MongoUniqueStringIndex

 public class DynamicFieldConfig
{
    public string InputType { get; set; }
    [MongoUniqueStringIndex]
    public string Name { get; set; }
    public string Label { get; set; }
    public List<string> Options { get; set; } = new List<string>();
    public string LookupTypeName { get; set; }
    public int Type { get; set; } //int to enum
    public string Value { get; set; }
    public List<DynamicFieldValidation> Validations { get; set; } = new List<DynamicFieldValidation>();
}

然后在哪里获取集合,例如在我的数据访问类中,我这样做:

        public MongoDataAccess(IDatabaseSettings settings)
    {
        // todo: create a connection service/factory to get connection
        var client = new MongoClient(settings.ConnectionString);
        var database = client.GetDatabase(settings.DatabaseName);
        
        var entityName = new Pluralizer().Pluralize(typeof(TMongoEntity).Name);
        _entityStore = database.GetCollection<TMongoEntity>(entityName);

        var uniqueStringIndexProperties = typeof(TMongoEntity).GetProperties().Where(
            prop => Attribute.IsDefined(prop, typeof(MongoUniqueStringIndex))).ToList();
        if (uniqueStringIndexProperties.Any())
            foreach (var propertyInfo in uniqueStringIndexProperties)
            {
                var options = new CreateIndexOptions { Unique = true };
                var propertyInfoName = propertyInfo.Name;
                var field = new StringFieldDefinition<TMongoEntity>(propertyInfoName);
                var indexDefinition = new IndexKeysDefinitionBuilder<TMongoEntity>().Ascending(field);
                var indexModel = new CreateIndexModel<TMongoEntity>(indexDefinition, options);
                _entityStore.Indexes.CreateOne(indexModel);
            }
    }

答案 6 :(得分:0)

我正在使用 <PackageReference Include="MongoDB.Driver" Version="2.11.5" />

这是我创建唯一索引的代码

var client = new MongoClient(mongourl);
var database = client.GetDatabase(dbName);

IMongoCollection<CollectionModel> myCollection = database.GetCollection<CollectionModel>("collection_name");

myCollection .Indexes.CreateOne(
      new CreateIndexModel<CollectionModel>(Builders<CollectionModel>.IndexKeys.Descending(model => model.YOUR_PROPERTY),
      new CreateIndexOptions { Unique = true }));