Search code examples
mongodbmongodb-.net-driver

Best way to create a text index on a MongoDB collection via C# driver


Using v1.9.0 of the C# driver (latest at time of writing) with MongoDB 2.6.0

What is the best way currently to create a text index on a collection, via the C# driver?

From what I could tell, it's not possible via MongoCollection.CreateIndex? So currently creating it using MongoDatabase.Eval like so:

Database.Eval(new EvalArgs { Code = "function(){db.dummycollection.ensureIndex({\"$**\" : \"text\"},{name:\"TextIndex\"});}"

Am I missing something / is there a better way?


Solution

  • This should work:

    collection.EnsureIndex(IndexKeys.Text("a", "b").Ascending("c"), IndexOptions.SetTextLanguageOverride("idioma").SetName("custom").SetTextDefaultLanguage("spanish"));
    

    https://jira.mongodb.org/browse/CSHARP-874

    https://github.com/mongodb/mongo-csharp-driver/commit/1e7db3bedb3bee1b0ccecdb5f8ff39854526213a