MongoDB indexing using c# driver

Foridul Islam
2 min readAug 4, 2022

--

In every project, there have some queries that can make the performance slower. MongoDB indexing can support for efficient execution of queries and help us to get the expected performance.

MongoDB indexing

The indexes in MongoDB are the special data structures that store some information related to the documents such that it becomes easy for MongoDB to find the right data items. The indexes are ordered by the value of the field specified in the index.

The main reason to add MongoDB indexing is that without indexing, the database must scan every item in the collection or table to find out the items that satisfy the query. If there exists any index, then the database can use the index and it can limit the query items.

Creating Index

MongoDB provides a method called createIndex() that allows users to create an index.

Syntax:

db.COLLECTION_NAME.createIndex({KEY:1})

Example:

db.Movies.createIndex({“Name”:1});

There is another process to create an index using the C# mongo driver. To do this we have some steps.

  1. MongoCollection

This represents the types of collection in MongoDB. We just need to put the connectionString and databaseName in MongoClient to get the MongoCollection.

IMongoCollection<T> mongoCollection = new MongoClient(connectionString).GetDatabase(databaseName);var collection = db.GetCollection<T>(typeof(T).Name + ‘s’);

2. FieldDefinition and Builders

FieldDefinition<TDocument> define how to get a field name. They are implicitly convertible from a string so that you can simply pass the field name you’d like.

FieldDefinition<T> fieldDefinition = "Title";

3. Index Keys Definition Builder

There have several types of IndexKeys builders.

(i) Text Index Key builder

The following method creates a text index key definition.

var indexKeysDefinition = Builders<T>.IndexKeys.Text(fieldDefinition);

(ii) Ascending index key definition

The following method creates an ascending index key definition.

var indexKeysDefinition = Builders<T>.IndexKeys.Ascending(fieldDefinition);

(iii) Descending index key definition

The following method creates a descending index key definition.

var indexKeysDefinition = Builders<T>.IndexKeys.Descending(fieldDefinition);

(iv) Geo2DSphere index key definition

The following method creates a Geo2DSphere index key definition.

var indexKeysDefinition = Builders<T>.IndexKeys.Geo2DSphere(fieldDefinition);

There are also some types of index key definition methods like Hashed, Geo2D, GeoHaystack, Combine, etc.

4. Create Index Async

We can use CreateOneAsync to create an index in a MongoDB collection.

return await mongoCollection.Indexes.CreateOneAsync(new CreateIndexModel<T>(indexKeysDefinition));

We can also use CreateOne.

To create multiple indexes, we can use CreateManyAsync or CreateMany.

5. Drops an index

The DropOneAsync method can drop and index from the Mongo collection.

Summary

As we know index can enhance the performance of a query dramatically. But sometimes it's very difficult to run an index creation script in a database especially if it is in a production environment. In that case, I hope this solution will be helpful.

--

--

Foridul Islam
Foridul Islam

Written by Foridul Islam

Sr. Software Engineer at SELISE Digital Platforms

No responses yet