Beispiel #1
0
        private void EnsureIndexes()
        {
            var database = new MongoClient(_connectionString).GetServer().GetDatabase(_databaseName);
            var collectionNames = database.GetCollectionNames();

            foreach (var collectionName in collectionNames)
            {
                var collection = database.GetCollection(collectionName);
                var indexesForCollection = _indexBuilder.Value.Get().Where(t => t.CollectionName.Equals(collectionName)).ToList();

                foreach (var mongoDbIndex in indexesForCollection)
                {
                    if (mongoDbIndex.IndexOptions.IsSome)
                        collection.CreateIndex(mongoDbIndex.Index, mongoDbIndex.IndexOptions.Value);
                    else
                        collection.CreateIndex(mongoDbIndex.Index);
                }
            }
        }