MongoDB Create index using C# .NET driver

Create MongoDB unique index using C driverMongoDB Create index using C NET driver

Today in this article, we shall see how to perform the MongoDB Create index using C# .NET driver.

As we understood in our previous article, the importance of indexes. Indexes can be created using CLI like mongo shell or UI interfaces like a compass or robomongo UI client or using code like node.js or C# mongo driver.

Today in this article, we will cover below aspects,

If you want to understand how to use MongoDB Compass UI to create indexes, Please use below article

Getting started

Please create any .NET/.NET Core application.

Please add the MongoDB driver NuGet package using the Nuget Package manager,

PM> Install-Package MongoDB.Driver -Version 2.9.3

Note: Please use the latest driver available

Step I – Establish the connection to the Database using MongoDB driver,

var _mongoClient = new MongoClient("mongodb://your connection string");
var db = _mongoClient.GetDatabase("your database name");

If using ASP.NET Core, please use dependency injection for injecting the driver instance,

Step2 – Create index script

Command

 { createIndexes: '<collection-name>', indexes: [ { key: { <indexed-fields-name>: 1 }, name: 'indexed-fields-name-custom', unique: true } ] }

Example

var commandStr = "{ createIndexes: 'TheCodeBuzzLib', indexes: [ { key: { AuthorId: 1 }, name: 'author-id', unique: true } ] }";

Step3 – Execute the script

 var result = _mongoClient.GetDatabase("TheCodeBuzzLib").RunCommand<BsonDocument>(commandStr);

Additionally, if needed we can use parse commands to parse the script,

 var cmd = BsonDocument.Parse(commandStr );
_mongoClient.GetDatabase("TheCodeBuzzLib").RunCommand<BsonDocument>(cmd);

Finally, you shall see indexes got for “author-id ” created successfully,

Create MongoDB indexes net

Approach 2

We can create indexes using another approach discussed in the below article

References:

Do you have any comments or ideas or any better suggestions to share?

Please sound off your comments below.

Happy Coding !!



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



3 thoughts on “MongoDB Create index using C# .NET driver

    1. Hi Pat, Thanks for the query.
      I see your query might not be formatted correctly.Could you try below query,
      var commandStr = “{ createIndexes: ‘static_data_tst’, indexes:[ { key: { physicalDatastoreName: 1 }, name: ‘DatastoreName’, unique: true } ] }”;
      var result = db.RunCommand(commandStr);

      You can even parse the command as mentioned above.

      For any issue please try this apporach- https://www.thecodebuzz.com/create-mongodb-indexes-csharp-net/

  1. Running Mongo Driver 2.14.1 I can’t get the above code to work. Here is my code:

    commandStr = “{ createIndexes: ‘static_data_tst’, indexes: [ { key: { physicalDatastoreName: 1 }, name: ‘DatastoreName’, unique: true } ] }”;
    var result = destDb.RunCommand(commandStr);

    I get this error message:

    Ultimately, I am trying to copy Indexes from one collection to another. If there is a better way please let me know!

Leave a Reply

Your email address will not be published. Required fields are marked *