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
- MongoCollection mongoCollection = mongoDatabase.GetCollection<Symbol>("Symbols");
Example-1 : Creating Collection Using CreateIndex() Method
- IndexKeysBuilder Key = IndexKeys.Ascending("abc");
- IndexOptionsBuilder options = IndexOptions.SetUnique(true).SetDropDups(true);
- mongoCollection.CreateIndex(Key, options);
- mongoCollection.CreateIndex(Key, options);
Example- 2: Creating Collection Using EnsureIndex
- mongoCollection.EnsureIndex(new IndexKeysBuilder().Ascending("abc"), IndexOptions.SetUnique(true));