Create Index in MongoDb

Introduction
There are different way available to create index in the MongoDB collection, here i have created in two way. One is using CreateIndex Method of collection and other is EnsureIndex method. See the examples below
 Object of collection
  1. MongoCollection mongoCollection = mongoDatabase.GetCollection<Symbol>("Symbols");   
Example-1 : Creating Collection Using CreateIndex() Method 
  1. IndexKeysBuilder Key = IndexKeys.Ascending("abc");  
  2. IndexOptionsBuilder options = IndexOptions.SetUnique(true).SetDropDups(true);  
  3. mongoCollection.CreateIndex(Key, options);  
  4. mongoCollection.CreateIndex(Key, options);  
 Example- 2: Creating Collection Using EnsureIndex
    
  1. mongoCollection.EnsureIndex(new IndexKeysBuilder().Ascending("abc"), IndexOptions.SetUnique(true));  
 
Ebook Download
View all
Learn
View all