正文html
由於MongoDB查詢速度快,又有查詢語法支持,作一些快速查詢仍是蠻方便的,可是調用它,又得重寫查詢實現mongodb
太麻煩,CRL來統一管理,和關係型數據庫同樣查詢了數據庫
此功能依賴MongoDB官方驅動MongoDB.Driver緩存
簡單示例框架
對象定義MongoDBModel.cs分佈式
public class MongoDBModel:CRL.IModel { public MongoDBModel() { //保持惟一 Id = new Guid(); } public Guid Id { get; set; } public string OrderId { get; set; } public int Status { get; set; } }
管理類實現MongoDBTestManageide
public class MongoDBTestManage : CRL.BaseProvider<MongoDBModel> { public static MongoDBTestManage Instance { get { return new MongoDBTestManage(); } } }
數據鏈接建立函數
CRL.SettingConfig.GetDbAccess = (dbLocation) =>
{
//可按type區分數據庫
var type2 = dbLocation.ManageType; if (type2 == typeof(Code.MongoDBTestManage)) { //實現MongoDB鏈接 return new CoreHelper.MongoDBHelper("mongodb://localhost:27017", "test2"); } return WebTest.Code.LocalSqlHelper.TestConnection; };
建立訪問對象post
var instance = Code.MongoDBTestManage.Instance;
插入數據大數據
instance.Add(new Code.MongoDBModel() { OrderId = "1212", Status = DateTime.Now.Second });
函數Count統計
int count = instance.Count(b => b.Status >= 0);
Group
var query = instance.GetLambdaQuery(); //group query.GroupBy(b => b.OrderId).Select(b => new { count = b.Status.SUM(), count2 = b.Status.COUNT() }); var list = query.ToDynamic(); foreach (var item in list) { var a = item.count; var key = item.OrderId; }
標準查詢
除了表明SQL特性的語法和函數不支持,其它都支持
var query2 = instance.GetLambdaQuery(); query2.Select(b => new { aa = b.Id, bb = b.Status }); query2.Where(b=>b.Status.In(1,2,3,4)&&b.OrderId.StartsWith("123"));//支持擴展方法 var result = query2.ToDictionary<Guid, int>();//返回字典 var result2 = query2.ToDynamic();//返回動態對象 var result3 = query2.ToList();//返回List<MongoDBModel>
更新刪除和以前調用方式保持一致
因爲MongoDB的特性,如下不能實現,調用可能會拋出異常
因爲驅動的問題,匿名對象結果無法實現,4.2新增的功能不被支持