|
@@ -74,7 +74,7 @@ namespace WingCloudMongoDB.Mongodb.Driver
|
|
|
/// </summary>
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
/// <param name="data"></param>
|
|
|
- public string InsertOne<T>(MongoTable data) where T : class, IMongoBaseData
|
|
|
+ public async Task<string> InsertOneAsync<T>(MongoTable data) where T : class, IMongoBaseData
|
|
|
{
|
|
|
var dataType = typeof(T);
|
|
|
var props = dataType.GetProperties();
|
|
@@ -112,7 +112,7 @@ namespace WingCloudMongoDB.Mongodb.Driver
|
|
|
{
|
|
|
throw new Exception($"{result.FirstOrDefault().Key} is duplicate key,value is {result.FirstOrDefault().Value}");
|
|
|
}
|
|
|
- collect.InsertOne(data);
|
|
|
+ await collect.InsertOneAsync(data);
|
|
|
return data.Id;
|
|
|
}
|
|
|
|
|
@@ -123,10 +123,15 @@ namespace WingCloudMongoDB.Mongodb.Driver
|
|
|
/// </summary>
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
/// <param name="id"></param>
|
|
|
- public void DeleteOne<T>(string key,string value) where T : class, IMongoBaseData
|
|
|
+ public async Task<bool> DeleteOneAsync<T>(string key,string value) where T : class, IMongoBaseData
|
|
|
{
|
|
|
var collect = GetCollection<T>();
|
|
|
- collect.DeleteOne(f => f.QueryData.QueryItems.Any(q => q.Key == key && q.Value == value));
|
|
|
+ var result = await collect.DeleteOneAsync(f => f.QueryData.QueryItems.Any(q => q.Key == key && q.Value == value));
|
|
|
+ if (result.DeletedCount > 0)
|
|
|
+ {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
@@ -135,11 +140,12 @@ namespace WingCloudMongoDB.Mongodb.Driver
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
/// <param name="id"></param>
|
|
|
/// <param name="data"></param>
|
|
|
- public void Update<T>(string key, string value, MongoTable data) where T : class, IMongoBaseData
|
|
|
+ public async Task<bool> Update<T>(string key, string value, MongoTable data) where T : class, IMongoBaseData
|
|
|
{
|
|
|
var collect = GetCollection<T>();
|
|
|
var update = Builders<MongoTable>.Update.Set(f => f.BaseData, data.BaseData).Set(f => f.ExtendsData, data.ExtendsData);
|
|
|
- collect.UpdateOne(f => f.QueryData.QueryItems.Any(q => q.Key == key && q.Value == value), update);
|
|
|
+ var result = await collect.UpdateOneAsync(f => f.QueryData.QueryItems.Any(q => q.Key == key && q.Value == value), update);
|
|
|
+ return result.ModifiedCount > 0;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
@@ -206,7 +212,7 @@ namespace WingCloudMongoDB.Mongodb.Driver
|
|
|
/// <param name="key"></param>
|
|
|
/// <param name="value"></param>
|
|
|
/// <returns></returns>
|
|
|
- public T FindOne<T>(string key, string value) where T : class, IMongoBaseData
|
|
|
+ public async Task<T> FindOneAsync<T>(string key, string value) where T : class, IMongoBaseData
|
|
|
{
|
|
|
var collect = GetCollection<T>();
|
|
|
var filter = Builders<MongoTable>.Filter.Empty;
|
|
@@ -227,8 +233,8 @@ namespace WingCloudMongoDB.Mongodb.Driver
|
|
|
Builders<MongoDataItem>.Filter.Regex(q => q.Value, $".*{value}.*")
|
|
|
)
|
|
|
));
|
|
|
- var userInfo = collect.Find(filter).FirstOrDefault();
|
|
|
- return (T)userInfo?.BaseData;
|
|
|
+ var userInfo = await collect.FindAsync(filter);
|
|
|
+ return (T)userInfo.Current.FirstOrDefault()?.BaseData;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
@@ -238,10 +244,16 @@ namespace WingCloudMongoDB.Mongodb.Driver
|
|
|
/// <param name="key"></param>
|
|
|
/// <param name="value"></param>
|
|
|
/// <returns></returns>
|
|
|
- public List<MongoTable> LikeFind<T>(string key, string value) where T : class, IMongoBaseData
|
|
|
+ public async Task<List<MongoTable>> LikeFindAsync<T>(string key, string value) where T : class, IMongoBaseData
|
|
|
{
|
|
|
+ var response = new List<MongoTable>();
|
|
|
var collect = GetCollection<T>();
|
|
|
- return collect.Find(f => f.QueryData.QueryItems.Any(q => q.Key == key && q.Value.ToUpper().IndexOf(value.ToUpper()) > -1)).ToList();
|
|
|
+ var cursor = await collect.FindAsync(f => f.QueryData.QueryItems.Any(q => q.Key == key && q.Value.ToUpper().IndexOf(value.ToUpper()) > -1));
|
|
|
+ while (await cursor.MoveNextAsync())
|
|
|
+ {
|
|
|
+ response.AddRange(cursor.Current);
|
|
|
+ }
|
|
|
+ return response;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
@@ -250,7 +262,7 @@ namespace WingCloudMongoDB.Mongodb.Driver
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
/// <param name="request"></param>
|
|
|
/// <returns></returns>
|
|
|
- public PageCollection<T> GetPage<T>(PageRequest request) where T : class, IMongoBaseData
|
|
|
+ public async Task<PageCollection<T>> GetPageAsync<T>(PageRequest request) where T : class, IMongoBaseData
|
|
|
{
|
|
|
var collect = GetCollection<T>();
|
|
|
List<FilterDefinition<MongoTable>> definitionList = new List<FilterDefinition<MongoTable>>();
|
|
@@ -272,14 +284,21 @@ namespace WingCloudMongoDB.Mongodb.Driver
|
|
|
{
|
|
|
function = Builders<MongoTable>.Filter.And(definitionList.ToArray());
|
|
|
}
|
|
|
- var query = collect.Find(function);
|
|
|
- var pageQuery = query.Skip((request.CurrentPage-1)*request.PageSize).Limit(request.PageSize);
|
|
|
- var data = pageQuery.ToList();
|
|
|
+ var response = new List<MongoTable>();
|
|
|
+ var count = await collect.CountDocumentsAsync(function);
|
|
|
+ var cursor = await collect.FindAsync(function, new FindOptions<MongoTable> { NoCursorTimeout = true, BatchSize = request.PageSize });
|
|
|
+ var cursorPage = request.CurrentPage - 1;
|
|
|
+ int eachPageIndex = 0;
|
|
|
+ while (eachPageIndex <= cursorPage)
|
|
|
+ {
|
|
|
+ await cursor.MoveNextAsync();
|
|
|
+ response.AddRange(cursor.Current);
|
|
|
+ }
|
|
|
var pageCollection = new PageCollection<T>();
|
|
|
pageCollection.CurrentPage = request.CurrentPage;
|
|
|
pageCollection.PageSize = request.PageSize;
|
|
|
- pageCollection.DataCount = data.Count;
|
|
|
- pageCollection.PageData = data.Select(f=>f.BaseData as T).ToList();
|
|
|
+ pageCollection.DataCount = (int)count;
|
|
|
+ pageCollection.PageData = response.Select(f=>f.BaseData as T).ToList();
|
|
|
return pageCollection;
|
|
|
}
|
|
|
|