/// <summary> /// 根据主键id删除数据库实体 /// </summary> /// <typeparam name="DbModel">关联的实体对象信息</typeparam> /// <param name="tableName">表名称</param> /// <param name="id">主键值</param> /// <returns>删除成功返回true;否则返回false</returns> public static Boolean DeleteDbModel <DbModel>(String tableName, String id) where DbModel : NoSqlBaseModel { FilterDefinition <DbModel> filter = Builders <DbModel> .Filter.Eq("_id", id); IMongoCollection <DbModel> collection = MongoDBHelper.GetMongoCollection <DbModel>(tableName); return(collection.DeleteOne(filter).DeletedCount > 0); }
/// <summary> /// 保存数据库实体 /// </summary> /// <typeparam name="DbModel">关联的实体对象信息</typeparam> /// <param name="tableName">表名称</param> /// <param name="dbModel">要保存的实体</param> /// <returns>保存成功返回实体对象;否则返回null</returns> public static DbModel SaveDbModel <DbModel>(String tableName, DbModel dbModel) where DbModel : NoSqlBaseModel { // 获取主键信息;组件主键过滤的查询条件 String id = Convert.ToString(dbModel.Id); FilterDefinition <DbModel> filter = Builders <DbModel> .Filter.Eq("_id", id); // 获取集合对象 IMongoCollection <DbModel> collection = MongoDBHelper.GetMongoCollection <DbModel>(tableName); // 判断数据对象是否存在,如果存在则更新,否则插入 DbModel tmpDbModel = collection.FindOneAndReplace(filter, dbModel); if (tmpDbModel == null) { collection.InsertOne(dbModel); } tmpDbModel = collection.Find(filter).FirstOrDefault(); // 返回保存后的数据 return(tmpDbModel); }