I want to verify that the mongoose query that I’m creating is using my indexes. Is there a way that I can view the final query that is generated to mongodb so that I can run a .explain() on the query?
I can guess what the query it is generating is, but just wanted to verify.
e.g.
var query = Post.find()
.regex('lowerCaseTitle', searchRegEx)
.$gte('status',0)
.$lt('start', now)
.$gt('end',now)
.sort('total', -1)
.limit(50);
One way is to use the mongodb profiler and set it to log all operations:
http://www.mongodb.org/display/DOCS/Database+Profiler
I’m not sure if there’s a simpler way to do it through mongoose itself, but that would be nice.
Update: Adding to what Dan said in the other answer’s comment, you should turn the profiler on to get what you want, and turn it back off. Leaving it as “log all operations” is definitely a good way to slow down your system. Limiting it to a development environment is a good idea, also.