@@ -13,11 +13,11 @@ namespace GameFrameX.DataBase.Mongo;
1313public sealed partial class MongoDbService
1414{
1515 /// <summary>
16- /// 删除数据
16+ /// 根据条件删除单条数据(软删除)
1717 /// </summary>
18- /// <param name="filter">查询条件 </param>
19- /// <typeparam name="TState"></typeparam>
20- /// <returns></returns>
18+ /// <param name="filter">查询条件表达式 </param>
19+ /// <typeparam name="TState">数据类型,必须继承自BaseCacheState </typeparam>
20+ /// <returns>返回修改的记录数 </returns>
2121 public async Task < long > DeleteAsync < TState > ( Expression < Func < TState , bool > > filter ) where TState : BaseCacheState
2222 {
2323 var state = await FindAsync ( filter ) ;
@@ -28,10 +28,52 @@ public async Task<long> DeleteAsync<TState>(Expression<Func<TState, bool>> filte
2828 }
2929
3030 /// <summary>
31- /// 删除一条数据
31+ /// 根据条件批量删除数据(软删除)
3232 /// </summary>
33- /// <param name="state"></param>
34- /// <typeparam name="TState"></typeparam>
33+ /// <param name="filter">查询条件表达式</param>
34+ /// <typeparam name="TState">数据类型,必须继承自BaseCacheState</typeparam>
35+ /// <returns>返回修改的记录数</returns>
36+ public async Task < long > DeleteListAsync < TState > ( Expression < Func < TState , bool > > filter ) where TState : BaseCacheState
37+ {
38+ var bulkUpdate = _mongoDbContext . Update < TState > ( ) ;
39+ var list = await FindListAsync ( filter ) ;
40+ var deleteTime = TimeHelper . UnixTimeMilliseconds ( ) ;
41+ foreach ( var state in list )
42+ {
43+ state . DeleteTime = deleteTime ;
44+ state . IsDeleted = true ;
45+ bulkUpdate . MatchID ( state . Id ) . Modify ( x => x . IsDeleted , state . IsDeleted ) . Modify ( x => x . DeleteTime , state . DeleteTime ) . AddToQueue ( ) ;
46+ }
47+
48+ var result = await bulkUpdate . ExecuteAsync ( ) ;
49+ return result . ModifiedCount ;
50+ }
51+
52+ /// <summary>
53+ /// 根据ID列表批量删除数据(软删除)
54+ /// </summary>
55+ /// <param name="ids">要删除的ID列表</param>
56+ /// <typeparam name="TState">数据类型,必须继承自BaseCacheState</typeparam>
57+ /// <returns>返回修改的记录数</returns>
58+ public async Task < long > DeleteListIdAsync < TState > ( IEnumerable < long > ids ) where TState : BaseCacheState
59+ {
60+ var bulkUpdate = _mongoDbContext . Update < TState > ( ) ;
61+ var deleteTime = TimeHelper . UnixTimeMilliseconds ( ) ;
62+ foreach ( var id in ids )
63+ {
64+ bulkUpdate . MatchID ( id ) . Modify ( x => x . IsDeleted , true ) . Modify ( x => x . DeleteTime , deleteTime ) . AddToQueue ( ) ;
65+ }
66+
67+ var result = await bulkUpdate . ExecuteAsync ( ) ;
68+ return result . ModifiedCount ;
69+ }
70+
71+ /// <summary>
72+ /// 删除指定对象(软删除)
73+ /// </summary>
74+ /// <param name="state">要删除的对象</param>
75+ /// <typeparam name="TState">数据类型,必须继承自BaseCacheState</typeparam>
76+ /// <returns>返回修改的记录数</returns>
3577 public async Task < long > DeleteAsync < TState > ( TState state ) where TState : BaseCacheState
3678 {
3779 state . DeleteTime = TimeHelper . UnixTimeMilliseconds ( ) ;
@@ -414,9 +456,9 @@ public async Task<long> DeleteManyAsync(string collName, FilterDefinition<BsonDo
414456 /// <summary>
415457 /// 删除一条记录
416458 /// </summary>
417- /// <typeparam name="TState"></typeparam>
418- /// <param name="filter">条件 </param>
419- /// <returns></returns>
459+ /// <typeparam name="TState">数据类型,必须继承自BaseCacheState </typeparam>
460+ /// <param name="filter">条件表达式 </param>
461+ /// <returns>返回被删除的记录 </returns>
420462 public TState DeleteOne<TState>(Expression<Func<TState, bool>> filter) where TState : BaseCacheState
421463 {
422464 var result = GetCollection<TState>().FindOneAndDelete(filter);
@@ -426,9 +468,9 @@ public TState DeleteOne<TState>(Expression<Func<TState, bool>> filter) where TSt
426468 /// <summary>
427469 /// 删除一条记录
428470 /// </summary>
429- /// <param name="collName">表名 </param>
430- /// <param name="filter">条件 </param>
431- /// <returns></returns>
471+ /// <param name="collName">集合名称 </param>
472+ /// <param name="filter">条件表达式 </param>
473+ /// <returns>返回被删除的记录 </returns>
432474 public BsonDocument DeleteOne(string collName, Expression<Func<BsonDocument, bool>> filter)
433475 {
434476 var result = GetCollection(collName).FindOneAndDelete(filter);
@@ -438,9 +480,9 @@ public BsonDocument DeleteOne(string collName, Expression<Func<BsonDocument, boo
438480 /// <summary>
439481 /// 删除一条记录
440482 /// </summary>
441- /// <typeparam name="TState"></typeparam>
442- /// <param name="filter">条件 </param>
443- /// <returns></returns>
483+ /// <typeparam name="TState">数据类型,必须继承自BaseCacheState </typeparam>
484+ /// <param name="filter">条件表达式 </param>
485+ /// <returns>返回被删除的记录 </returns>
444486 public async Task<TState> DeleteOneAsync<TState>(Expression<Func<TState, bool>> filter) where TState : BaseCacheState
445487 {
446488 var result = await GetCollection<TState>().FindOneAndDeleteAsync(filter);
@@ -450,9 +492,9 @@ public async Task<TState> DeleteOneAsync<TState>(Expression<Func<TState, bool>>
450492 /// <summary>
451493 /// 删除一条记录
452494 /// </summary>
453- /// <param name="collName">表名 </param>
454- /// <param name="filter">条件 </param>
455- /// <returns></returns>
495+ /// <param name="collName">集合名称 </param>
496+ /// <param name="filter">条件表达式 </param>
497+ /// <returns>返回被删除的记录 </returns>
456498 public async Task<BsonDocument> DeleteOneAsync(string collName, Expression<Func<BsonDocument, bool>> filter)
457499 {
458500 var result = await GetCollection(collName).FindOneAndDeleteAsync(filter);
0 commit comments