Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 361
AdaTheDev Avatar asked Apr 30 '14 08:04

AdaTheDev


2 Answers

Just verified that the following works with the 1.9.0 C# driver and MongoDB 2.6.0-rc2:

MongoCollection.CreateIndex(new IndexKeysDocument("Markdown", "text"));

(this also works with older drivers)

EDIT

djch's answer shows the better way to do it using the 1.9 driver because it can also be used stronly-typed, e.g.:

MongoCollection.CreateIndex(IndexKeys<MyClass>.Text(p => p.Markdown));
like image 124
mnemosyn Avatar answered Nov 15 '22 05:11

mnemosyn


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

like image 43
David Henderson Avatar answered Nov 15 '22 03:11

David Henderson