对MongoDB数据库进行性能分析以查看已执行的查询 [英] Profiling the MongoDB database to see the executed queries

查看:447
本文介绍了对MongoDB数据库进行性能分析以查看已执行的查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法查看MongoDB上已执行的查询?我使用以下命令在Windows上通过mongo.exe启用了分析功能:

Is there a way to see the executed queries on MongoDB? I enabled profiling through the mongo.exe on windows with the following command:

db.setProfilingLevel(2);

这将启用分析,例如,我可以使用以下命令查询配置文件数据:

This enables profiling and I can query the profile data with the following command for example:

db.system.profile.find().limit(10).sort( { ts : -1 } ).pretty()

但是,这不能让我执行查询.我知道我也可以使用IMongoQuery.ToJson()方法查看查询,但是我将Linq查询与MongoDB C#驱动程序一起使用(顺便说一句,我真的很奇怪为什么他们调用此C#驱动程序而不是.NET驱动程序).

However, this doesn't get me the executed queries. I know that I can also use the IMongoQuery.ToJson() method to see the query but I am using Linq queries with MongoDB C# Driver (BTW, I really wonder why they called this C# driver instead of .NET driver).

以下是示例:

var people = db.GetCollection<Person>("People")
    .AsQueryable().Where(x => x.Sessions.Any(y => y.SessionDate != null));

var peeps = people.Select(x => 
    x.Sessions.Where(y => y.SessionDate != null)).ToList();

但是,能够执行以下操作真的很酷:

However, that would be really cool to be able to do the following:

var people = db.GetCollection<Person>("People")
    .AsQueryable().Where(x => x.Sessions.Any(y => y.SessionDate != null))
    .Expression.ToJson();

但是我猜这不被支持.有什么想法吗?

But this is not supported I guess. Any ideas?

推荐答案

我认为没有得到IMongoQuery的方法是不可行的.好消息是您可以将people转换为MongoQueryable<Person>并从那里获取IMongoQuery:

I don't think there is a way to do it without getting the IMongoQuery. The good news is that you can cast people to a MongoQueryable<Person> and get the IMongoQuery from there:

var people = db.GetCollection<Person>("People")
    .AsQueryable().Where(x => x.Sessions.Any(y => y.SessionDate != null));

var mqPeople = (MongoQueryable<Person>)people;
var query = mqPeople.GetMongoQuery().ToJson();

看来,这仅适用于Where子句.

It looks like this will only work for the Where clause though.

这篇关于对MongoDB数据库进行性能分析以查看已执行的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆