How to create indexes in MongoDB via .NET

后端 未结 6 978
有刺的猬
有刺的猬 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:49

    you should use CreateIndex as EnsureIndex is marked obsolete for future compatibility with the next versions of MongoDB:

    var client = new MongoClient("mongodb://localhost");
    var db = client.GetServer().GetDatabase("db");
    var collection = db.GetCollection("Hamsters");
    
    collection.CreateIndex(IndexKeys.Ascending(_ => _.Name));
    

提交回复
热议问题