问题描述
有没有办法在 MongoDB 上查看已执行的查询?我使用以下命令在 Windows 上通过 mongo.exe 启用了分析:
db.setProfilingLevel(2);
db.system.profile.find().limit(10).sort( { ts : -1 } ).pretty()
但是,这并没有让我得到执行的查询.我知道我也可以使用 IMongoQuery.ToJson() 方法来查看查询,但我正在使用带有 MongoDB C# 驱动程序的 Linq 查询(顺便说一句,我真的很想知道他们为什么调用这个 C# 驱动程序而不是 .NET 驱动程序).
示例如下:
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();
但是,能够执行以下操作真的很酷:
var people = db.GetCollection<Person>("People") .AsQueryable().Where(x => x.Sessions.Any(y => y.SessionDate != null)) .Expression.ToJson();
但我猜这是不支持的.有什么想法吗?
推荐答案
我认为没有获得 IMongoQuery 就没有办法做到这一点.好消息是您可以将 people 转换为 MongoQueryable<Person> 并从那里获取 IMongoQuery:
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 子句.
问题描述
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()
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).
Here is the sample:
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?
推荐答案
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();
Edit:
It looks like this will only work for the Where clause though.