creating a compound index in c#

user2010435 picture user2010435 · Feb 15, 2013 · Viewed 7k times · Source

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

Answer

WiredPrairie picture WiredPrairie · Feb 16, 2013

Here's one way:

var keys = new IndexKeysBuilder();

keys.Ascending(keyName1);
keys.Descending(keyName2);

var options = new IndexOptionsBuilder();
options.SetSparse(true);
options.SetUnique(false);
collection.EnsureIndex(keys, options);