Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

creating a compound index in c#

I want to create a compound index where one key should be in ascending, the second key in descending order.

How can I do this?

I have a string containing the property names the user selected.

collection.EnsureIndex(IndexKeys.Descending(selectedProperties[0]),
                IndexKeys.Ascending(selectedProperties[1])),
IndexOptions.......

does not work

like image 307
user2010435 Avatar asked Feb 15 '13 22:02

user2010435


1 Answers

In v2.x of the driver they completely changed the API so currently the way to create a compound index asynchronously (which is prefered) is:

await collection.Indexes.CreateOneAsync(
    Builders<Hamster>.IndexKeys.Ascending(_ => _.Name).Descending(_ => _.Age),
    new CreateIndexOptions { Background = true });

And synchronously:

collection.Indexes.CreateOne(
    Builders<Hamster>.IndexKeys.Ascending(_ => _.Name).Descending(_ => _.Age),
    new CreateIndexOptions { Sparse = true });
like image 89
i3arnon Avatar answered Oct 01 '22 04:10

i3arnon