Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create indexes in MongoDB via .NET

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 that?

like image 605
Payedimaunt Avatar asked Jul 23 '13 10:07

Payedimaunt


People also ask

Can we create index in MongoDB?

So, MongoDB provides a createIndex() method to create one or more indexes on collections. Using this method we can create different types of indexes like text index, 2dsphere index, 2d index, etc.

How do you create an index on a field in MongoDB?

To create a wildcard index on all fields and subfields in a document, specify { "$**" : 1 } as the index key. You cannot specify a descending index key when creating a wildcard index.

How are MongoDB indexes implemented?

Without indexes, MongoDB must perform a collection scan, i.e. scan every document in a collection, to select those documents that match the query statement. If an appropriate index exists for a query, MongoDB can use the index to limit the number of documents it must inspect.

Which method is used for indexing in MongoDB?

You can find all the available indexes in a MongoDB collection by using the getIndexes method. This will return all the indexes in a specific collection. Result: The output contains the default _id index and the user-created index student name index.


3 Answers

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<Hamster>("Hamsters");

collection.CreateIndex(IndexKeys<Hamster>.Ascending(_ => _.Name));
like image 77
i3arnon Avatar answered Oct 09 '22 20:10

i3arnon


The overload of CreateOneAsync in the currently accepted answer is now marked as obsolete with the message "Use CreateOneAsync with a CreateIndexModel instead." Here's how you do it:

static async Task CreateIndex(string connectionString)
{
    var client = new MongoClient(connectionString);
    var database = client.GetDatabase("HamsterSchool");
    var collection = database.GetCollection<Hamster>("Hamsters");
    var indexOptions = new CreateIndexOptions();
    var indexKeys = Builders<Hamster>.IndexKeys.Ascending(hamster => hamster.Name);
    var indexModel = new CreateIndexModel<Hamster>(indexKeys, indexOptions);
    await collection.Indexes.CreateOneAsync(indexModel);
}
like image 38
Stephen Kennedy Avatar answered Oct 09 '22 21:10

Stephen Kennedy


Something like this should do:

var server = MongoServer.Create("mongodb://localhost");
var db = server.GetDatabase("myapp");

var users = db.GetCollection<User>("users");

users.EnsureIndex(new IndexKeysBuilder().Ascending("EmailAddress"));

Please see the following bits in the documentation:

  • http://api.mongodb.org/csharp/current/html/06bcd201-8844-3df5-d170-15f2b423675c.htm
like image 15
Derick Avatar answered Oct 09 '22 20:10

Derick