问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/13901822/profiling-the-mongodb-database-to-see-the-executed-queries