Jardalu
Jardalu

Reputation: 231

mongodb : How do I create text index with C# driver?

For mongodb, how can I create the following index in C# ?

db.reviews.ensureIndex( { comments: "text" } )

I don't see any "Text" option for IndexOptions at http://api.mongodb.org/csharp/current/?topic=html/7e62224e-33ab-098b-4e07-797c45494a63.htm

Upvotes: 3

Views: 2148

Answers (2)

Dĵ ΝιΓΞΗΛψΚ
Dĵ ΝιΓΞΗΛψΚ

Reputation: 5669

the easiest way to create text indexes in c# is by using the driver wrapper library MongoDB.Entities. here's an example of creating a text index:

    DB.Index<Review>()
      .Key(a => a.Comment, Type.Text)
      .Create();

haven't seen anything else that makes it simpler than that.

Upvotes: 0

WiredPrairie
WiredPrairie

Reputation: 59763

You'll need to set this up through a script or directly on the MongoDB database as the C# driver doesn't expose the text index creation feature as it's still in "beta".

Unfortunately, you can't easily override the behavior either ... as the classes that control the behavior aren't easily overriden/extensible.

If you created a copy of the IndexKeysBuilder class (here), and added a new method (something like below):

public IndexKeysBuilder Text(string name)
{
    _document.Add(name, "text");
    return this;
}

You could use that instead of the built in stuff and in theory, it should work (I've not tested this).

Upvotes: 1

Related Questions