MongoDB学习笔记~关于官方驱动集成IQueryable之后的一些事

回到目录

关于官方驱动集成IQueryable之后的一些事,有好事也有坏事,好事就是它会将你的linq语句非常友好的翻译成MongoDB语句,而坏事就是有一些linq语句不会被翻译,不会被翻译的代价就是将整个结果集装到内存,然后进行linq to object的查询,效率自然是非常低的,呵呵。

好事

最新官方驱动中,添加了对IQueryable扩展方法的支持

        public static IMongoQueryable<TDocument> AsQueryable<TDocument>(this IMongoCollection<TDocument> collection);

1 它对Where完成支持

_webManageUsersRepository.GetModel().Where(i => i.LoginName == "zzl");

2 它对group完全支持

 var g = _webManageUsersRepository.GetModel()
.GroupBy(i => i.DepartmentID, (i, v) => new
{
dept = i,
userCount = v.Count()
});

通过断点我们可以看到它所生成的mongodb语句,然后可以把语句放在命令行中去执行看看具体效果

db.WebManageUsers.aggregate([{ "$group" : { "_id" : "$DepartmentID", "userCount" : { "$sum" :  } } }])

上面写法是我比较喜欢的lambda表达式的方法,语法简介,漂亮,而我不太喜欢linq写法,但是,如果是多字段的分组,你就必须使用linq标准写法了,因为到目前为止,mongo官方驱动还不支持多字段分组的lambda写法,如下面的代码,分组结果就是错误的

 var bb = _webManageUsersRepository.GetModel()
.GroupBy(
i => new
{
i.DepartmentID,
i.Status
},
(i, v) => new
{
dept = i.DepartmentID,
status = i.Status,
userCount = v.Count()
});

它不能正确的翻译成Mongo表达式

{aggregate([{ "$group" : { "dept" : "$DepartmentID", "status" : "$Status", "userCount" : { "$sum" :  }, "_id" :  } }])}

而使用传统的linq写法就可以被mongo驱动很好的翻译

  var gg = from a in _webManageUsersRepository.GetModel()
group a by new
{
dept = a.DepartmentID,
status = a.Status
} into g
select new RoleCount
{
dept = g.Key.dept,
status = g.Key.status,
userCount = g.Count()
};

下面生成的代码是正确的

aggregate([{ "$group" : { "_id" : { "dept" : "$DepartmentID", "status" : "$Status" }, "__agg0" : { "$sum" :  } } }, { "$project" : { "dept" : "$_id.dept", "status" : "$_id.status", "userCount" : "$__agg0", "_id" :  } }])}

可以在mongo客户端上看到正确的结果

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAz8AAACkCAIAAAA2fgcJAAAWZElEQVR4nO2dW5LlqA5FNXQPzUO7Hze6wslDFoiH8FmOFR2nSEBbG2xUzqxsue9b5LJw37el87PPv8/FgbXZ8va+OZMOyh9f8zJKsif7mpEuqTW6cbg9zVrP53+b0mxazY4dYjHZuEuHR7enWZOtLLpl8toMnlsGAAAmYn8K64dE3vPflbQUu9UG5p2V4cZJlDlf07Gn+ZpUq/hXk+0rZRep6CxGfPpQHP78Y/FzR3Q9I12Dkn6xsRjds+d1l/Ke+RLX+uRq9eiW3aVIAgCAdax5EBcPv9cTsWNOWMAMt+2ruXetj9tpxwkGAAADqyJZXjkMmRMOhdXETAAAsLFfAQAAAADY2a8AAAAAAOzsVwAAAAAAdvYrgJ/A+COPxp/Wuv/+apIITJK00ZDvZXS6dR/D6VJAk395L1l0Dvlh3ICGbHpU7k4bfoFilWbsWetjn3NZgjMEfKx025jRdutCnTfboXTzzBkq8abcncp/ZN1NXrWG/FcM2ovo5xB9YPFL939X8nnZwmyM/hnsT59Xe5XH2a7VmfpA6TakdcKne5EzGrjKM9I07s/7cc0weQv330tJ0+n81P0ZR1LTs27jjmrK3SNv+7oP38k+r9oDJ4/4VtP1UcWvJs+C9QtmdvOHaF194/A+h+0b7DhWprMmlj+K89RfT3F/djxI99punLN4vG00vyl0/KeHvpcU/4PkHt/hV+XLUniv3lrVUL1BkxVUb4nUvtttjbYjMtpSvXkePq0n7gy2V29r0nTmvqyCmbeX4ue+IMps2TEelWY19+Oqtegi8sM1H16c898fa531bsVAeqy78sQxSs0tUsRb3GsV/4yoR+8zxNhT2Vr2RiO10H2GFD/XEvfrVDp3G1IL/epS2Iwsu662Rv618yx6cXgirzUjpaeUNnD+VfucuX59BkuaM8gl1XQqLYqHSqC9e6nobczcF2yDV+vuyp5X0syHzF53Q09bv7t0BxYbleGJC/Y5m4bnHfRAtSy6dRY/K2tvsa5JfH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oJf6AUAAABwEnIJBRwAAADAMfz3iQIOAAAA4ATkEko3AAAAgGOgdAMAAAA4CEo3AAAAgKPYrwAAAAAA7OxXAAAAAAB29isAAAAAADv7FQAAAACAlf8BIdq0b5NcJl4AAAAASUVORK5CYII=" alt="" />

坏事

对count()方法完成不支持,不推荐使用,如果要用到count(),建议使用mongo原生态的,而不是linq的

            Stopwatch sw1 = new Stopwatch();
sw1.Restart();
var a1 = _webManageUsersRepository.Count(i => true);//性能好
sw1.Stop();
var at1 = sw1.ElapsedMilliseconds; Stopwatch sw = new Stopwatch();
sw.Restart();
var a = _webManageUsersRepository.GetModel().Count();//性能差
sw.Stop();
var at = sw.ElapsedMilliseconds;

回到目录

上一篇:MFC之常用控件(四)


下一篇:Markdown 完全指南