creating a compound index in c#
.net, c#, compound-index, mongodb, mongodb-.net-driver
Solution
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 });
Problem
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