同步移除一条数据
设定好移除条件FilterDefinition
后,使用collection.DeleteOne
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| #region 移除数据
public DeleteResult Delete<T>(string collName, string objectId) where T : class, new() { try { var collection = m_Database?.GetCollection<T>(collName); FilterDefinition<T> filter = Builders<T>.Filter.Eq("_id",new ObjectId(objectId)); return collection!.DeleteOne(filter); } catch (Exception ex) { Debug.LogError("移除数据出错" + ex); throw; } }
|
异步移除一条数据
使用collection.DeleteOneAsync
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
public async Task<DeleteResult> DeleteAsync<T>(string collName, string objectId) where T : class, new() { try { var collection = m_Database?.GetCollection<T>(collName); FilterDefinition<T> filter = Builders<T>.Filter.Eq("_id", new ObjectId(objectId)); return await collection!.DeleteOneAsync(filter); } catch (Exception ex) { Debug.LogError("异步移除数据出错" + ex); throw; } }
|
同步批量移除数据
使用collection.DeleteMany
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
public DeleteResult DeleteMany<T>(string collName,FilterDefinition<T> filter) where T : class, new() { try { var collection = m_Database?.GetCollection<T>(collName); return collection!.DeleteMany(filter); } catch (Exception ex) { Debug.LogError("批量移除数据出错:" + ex); throw; } }
|
异步批量移除数据
使用collection.DeleteManyAsync();
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
public async Task<DeleteResult> DeleteManyAsync<T>(string collName, FilterDefinition<T> filter) where T : class, new() { try { var collection = m_Database?.GetCollection<T>(collName); return await collection!.DeleteManyAsync(filter); } catch (Exception ex) { Debug.LogError("异步批量移除数据出错:" + ex); throw; } }
|