Advertisement
Guest User

Untitled

a guest
Feb 20th, 2016
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.66 KB | None | 0 0
  1.         public void EnsureCompoundIndex (List<KeyValuePair<string, bool>> fields,
  2.             string collectionName, bool setUnique = true)
  3.         {
  4.             var collection = _database.GetCollection<BsonDocument>(collectionName);
  5.             IndexKeysDefinition<BsonDocument> keys;
  6.             CreateIndexOptions options = new CreateIndexOptions();
  7.  
  8.  
  9.             foreach (KeyValuePair<string, bool> field in fields)
  10.                 if (field.Value == true)
  11.                     keys.Add.Ascending (field.Key);
  12.                 else
  13.                     keys.Add.Descending (field.Key);
  14.             }
  15.  
  16.             // Set options
  17.             if   (setUnique) options.Unique = true;
  18.             else             options.Unique = false;
  19.  
  20.             // Create index
  21.             collection.Indexes.CreateOne(keys, options);
  22.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement