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
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 });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With