Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void EnsureCompoundIndex (List<KeyValuePair<string, bool>> fields,
- string collectionName, bool setUnique = true)
- {
- var collection = _database.GetCollection<BsonDocument>(collectionName);
- IndexKeysDefinition<BsonDocument> keys;
- CreateIndexOptions options = new CreateIndexOptions();
- foreach (KeyValuePair<string, bool> field in fields)
- if (field.Value == true)
- keys.Add.Ascending (field.Key);
- else
- keys.Add.Descending (field.Key);
- }
- // Set options
- if (setUnique) options.Unique = true;
- else options.Unique = false;
- // Create index
- collection.Indexes.CreateOne(keys, options);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement