How to create indexes in MongoDB via .NET

后端 未结 6 898
有刺的猬
有刺的猬 2020-12-08 18:23

I\'ve programmatically created a new document collection using the MongoDB C# driver.

At this point I want to create and build indexes programmatically. How can I do

6条回答
  •  有刺的猬
    2020-12-08 18:56

    Something like this should do:

    var server = MongoServer.Create("mongodb://localhost");
    var db = server.GetDatabase("myapp");
    
    var users = db.GetCollection("users");
    
    users.EnsureIndex(new IndexKeysBuilder().Ascending("EmailAddress"));
    

    Please see the following bits in the documentation:

    • http://api.mongodb.org/csharp/current/html/06bcd201-8844-3df5-d170-15f2b423675c.htm

提交回复
热议问题