//实例化
protected MongoDbHelper MongoDbHelper = new MongoDbHelper();
//collectionName即为mongodb中的对象名
public AjaxResult SaveForm<T>(T entity) where T : class
{
var result = new AjaxResult
{
type = (int)ResultType.Success,
message = "保存成功。"
};
try
{
MongoDbHelper.Insert(entity);
}
catch (Exception ex)
{
result.type = (int)ResultType.Success;
result.message = "保存失败。";
}
return result;
}
public AjaxResult SaveForm<T>(T entity, string collectionName) where T : class
{
var result = new AjaxResult
{
type = (int)ResultType.Success,
message = "保存成功。"
};
try
{
MongoDbHelper.Insert(entity, collectionName);
}
catch (Exception ex)
{
result.type = (int)ResultType.Success;
result.message = "保存失败。";
}
return result;
}
public AjaxResult SaveForm<T>(List<T> entitys) where T : class
{
var result = new AjaxResult
{
type = (int)ResultType.Success,
message = "保存成功。"
};
try
{
MongoDbHelper.Insert(entitys);
}
catch (Exception ex)
{
result.type = (int)ResultType.Success;
result.message = "保存失败。";
}
return result;
}
public AjaxResult SaveForm<T>(List<T> entitys, string collectionName) where T : class
{
var result = new AjaxResult
{
type = (int)ResultType.Success,
message = "保存成功。"
};
try
{
MongoDbHelper.Insert(entitys, collectionName);
}
catch (Exception ex)
{
result.type = (int)ResultType.Success;
result.message = "保存失败。";
}
return result;
}
public List<T> Find<T>(Expression<Func<T, bool>> condition, string sort) where T : class, new()
{
return MongoDbHelper.Get(condition, sort);
}
public List<T> Find<T>(List<Expression<Func<T, bool>>> conditions, string sort) where T : class, new()
{
return MongoDbHelper.Get(conditions, sort);
}
/// <summary>
/// 取符合条件的数据 sort中多个排序条件逗号分隔,默认asc
/// </summary>
public List<T> Find<T>(Expression<Func<T, bool>> condition, int skip, int limit, string sort) where T : class, new()
{
return MongoDbHelper.Get(condition, skip, limit, sort);
}
public AjaxResult RemoveForm<T>(Expression<Func<T, bool>> condition, string collectionName) where T : class
{
var result = new AjaxResult
{
type = (int)ResultType.Success,
message = "删除成功。"
};
try
{
MongoDbHelper.Delete(collectionName, condition);
}
catch (Exception ex)
{
result.type = (int)ResultType.Success;
result.message = "删除失败。";
}
return result;
}
public AjaxResult RemoveForm<T>(Expression<Func<T, bool>> condition) where T : class
{
var result = new AjaxResult
{
type = (int)ResultType.Success,
message = "删除成功。"
};
try
{
MongoDbHelper.Delete(condition);
}
catch (Exception ex)
{
result.type = (int)ResultType.Success;
result.message = "删除失败。";
}
return result;
}
public AjaxResult BatchRemoveForm<T>(List<Expression<Func<T, bool>>> conditions, string collectionName) where T : class
{
var result = new AjaxResult
{
type = (int)ResultType.Success,
message = "删除成功。"
};
try
{
MongoDbHelper.Delete(collectionName, conditions);
}
catch (Exception ex)
{
result.type = (int)ResultType.Success;
result.message = "删除失败。";
}
return result;
}
public AjaxResult BatchRemoveForm<T>(List<Expression<Func<T, bool>>> conditions) where T : class
{
var result = new AjaxResult
{
type = (int)ResultType.Success,
message = "删除成功。"
};
try
{
MongoDbHelper.Delete(conditions);
}
catch (Exception ex)
{
result.type = (int)ResultType.Success;
result.message = "删除失败。";
}
return result;
}