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
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));