public override void UpdateIndexes <T>(IMongoIndexManager <T> indexes) { if (!indexesUpdated) { lock (IndexLock) { if (!indexesUpdated) { var index = indexes.CreateOneAsync(Builders <T> .IndexKeys.Ascending("HospitalId")).Result; index = indexes.CreateOneAsync(Builders <T> .IndexKeys.Ascending("Name")).Result; indexesUpdated = true; } } } }
/// <summary> /// 更新索引 /// </summary> /// <param name="collection">集合名</param> /// <param name="index">索引键</param> /// <param name="asc"></param> /// <returns></returns> public async Task <string> UpdateIndexAsync(string collection, string index, bool asc = true) { IMongoIndexManager <BsonDocument> mgr = Database.GetCollection <BsonDocument>(collection).Indexes; return(await mgr.CreateOneAsync(new CreateIndexModel <BsonDocument>(asc ? Builders <BsonDocument> .IndexKeys.Ascending(doc => doc[index]) : Builders <BsonDocument> .IndexKeys.Descending(doc => doc[index])))); }
/// <summary> /// 更新索引 /// </summary> /// <param name="collection">集合名</param> /// <param name="key"></param> /// <param name="asc"></param> /// <returns></returns> public async Task <string> UpdateIndexAsync <T>(string collection, Expression <Func <T, object> > key, bool asc = true) { IMongoIndexManager <T> mgr = Database.GetCollection <T>(collection).Indexes; return(await mgr.CreateOneAsync( new CreateIndexModel <T>(asc ? Builders <T> .IndexKeys.Ascending(key) : Builders <T> .IndexKeys.Descending(key)))); }
/// <summary> /// 创建索引 /// </summary> /// <param name="collection">集合名</param> /// <param name="index">索引键</param> /// <param name="key"></param> /// <param name="asc"></param> /// <returns></returns> public async Task <string> CreateIndexAsync <T>(string collection, string index, Expression <Func <T, object> > key, bool asc = true) { IMongoIndexManager <T> mgr = Database.GetCollection <T>(collection).Indexes; var list = mgr.List(); while (list.MoveNext()) { if (!list.Current.Any(doc => doc["name"].AsString.StartsWith(index))) { return(await mgr.CreateOneAsync(new CreateIndexModel <T>(asc ? Builders <T> .IndexKeys.Ascending(key) : Builders <T> .IndexKeys.Descending(key)))); } } return(String.Empty); }
/// <summary> /// 创建索引 /// </summary> /// <param name="collection">集合名</param> /// <param name="index">索引键</param> /// <param name="asc"></param> /// <returns></returns> public async Task <string> CreateIndexAsync(string collection, string index, bool asc = true) { IMongoIndexManager <BsonDocument> mgr = Database.GetCollection <BsonDocument>(collection).Indexes; var list = mgr.List(); while (list.MoveNext()) { if (!list.Current.Any(doc => doc["name"].AsString.StartsWith(index))) { return(await mgr.CreateOneAsync(new CreateIndexModel <BsonDocument>(asc ? Builders <BsonDocument> .IndexKeys.Ascending(doc => doc[index]) : Builders <BsonDocument> .IndexKeys.Descending(doc => doc[index])))); } } return(string.Empty); }