Просмотр исходного кода

mongodb增加几个删除方法

Murphy 3 лет назад
Родитель
Сommit
50a40c0f61

+ 8 - 0
MicroServices/Business/Business.Core/MongoDBHelper/IMongoDB.cs

@@ -82,5 +82,13 @@ namespace Business.Core.MongoDBHelper
         /// <returns></returns>
         Task DeleteManyByCondition(Expression<Func<T, bool>> filter);
 
+        Task DeleteById(long id);
+
+        Task DeleteByIds(IEnumerable<long> ids);
+
+        Task<DeleteResult> Delete(Expression<Func<T, bool>> expression, bool isOne = false);
+
+        Task<DeleteResult> Delete(FilterDefinition<T> filter, bool isOne = false);
+
     }
 }

+ 35 - 1
MicroServices/Business/Business.Core/MongoDBHelper/MongoDBTools.cs

@@ -138,7 +138,41 @@ namespace Business.Core.MongoDBHelper
         /// <returns></returns>
         public Task DeleteManyByCondition(Expression<Func<T, bool>> filter)
         {
-            return mongoCollection.DeleteManyAsync<T>(filter);
+            return mongoCollection.DeleteManyAsync(filter);
+        }
+
+        /// <summary>
+        /// 根据id删除对象
+        /// </summary>
+        /// <param name="id"></param>
+        public Task DeleteById(long id)
+        {
+            return mongoCollection.DeleteManyAsync(s => id==s.Id);
+        }
+
+        /// <summary>
+        /// 根据id列表批量删除 对象
+        /// </summary>
+        /// <param name="ids">id列表</param>
+        public Task DeleteByIds(IEnumerable<long> ids)
+        {
+            return mongoCollection.DeleteManyAsync(s => ids.Contains(s.Id));
+        }
+
+        public Task<DeleteResult> Delete(Expression<Func<T, bool>> expression, bool isOne = false)
+        {
+            if (isOne)
+                return mongoCollection.DeleteOneAsync(expression);
+            else
+                return mongoCollection.DeleteManyAsync(expression);
+        }
+
+        public Task<DeleteResult> Delete(FilterDefinition<T> filter, bool isOne = false)
+        {
+            if (isOne)
+                return mongoCollection.DeleteOneAsync(filter);
+            else
+                return mongoCollection.DeleteManyAsync(filter);
         }
     }
 }