我想验证我正在创建的mongoose查询正在使用我的索引.有没有办法可以查看为mongodb生成的最终查询,以便我可以在查询上运行.explain()?
我可以猜出它生成的查询是什么,但只是想验证.
例如
var query = Post.find()
.regex('lowerCaseTitle', searchRegEx)
.$gte('status',0)
.$lt('start', now)
.$gt('end',now)
.sort('total', -1)
.limit(50);
Run Code Online (Sandbox Code Playgroud)
max*_*nes 48
您可以使用mongoose上的debug选项获取执行查询:
mongoose.set('debug', true);
Run Code Online (Sandbox Code Playgroud)
要么
mongoose.set('debug', function (collectionName, method, query, doc, options) {
//
});
Run Code Online (Sandbox Code Playgroud)
另外一个选项:
model
.find()
.{{...}} // any thing else
.setOptions({explain: 'executionStats'})
.exec((e, explainObj)=>{
...
})
Run Code Online (Sandbox Code Playgroud)
您也可以直接使用Query.prototype.explain()(如 Mongoose 5.2.13)
var query = Post.find()
.regex('lowerCaseTitle', searchRegEx)
.$gte('status',0)
.$lt('start', now)
.$gt('end',now)
.sort('total', -1)
.limit(50);
query.explain().then(console.log);
Run Code Online (Sandbox Code Playgroud)
Eve*_*man -2
一种方法是使用 mongodb profiler 并将其设置为记录所有操作:
http://www.mongodb.org/display/DOCS/Database+Profiler
我不确定是否有更简单的方法通过猫鼬本身来做到这一点,但这会很好。
更新:添加丹在其他答案的评论中所说的内容,您应该打开分析器以获得您想要的内容,然后将其关闭。将其保留为“记录所有操作”绝对是减慢系统速度的好方法。将其限制在开发环境中也是一个好主意。
| 归档时间: |
|
| 查看次数: |
10841 次 |
| 最近记录: |