解释(“executionStats”)与聚合在猫鼬中不起作用

Jon*_*Lee 5 mongoose

为了测试我的聚合在 mongoose 5.5.4(当前最新版本)中的查询性能,然后我调用

User.explain("executionStats").aggregate([]);
Run Code Online (Sandbox Code Playgroud)

它意外地调用“User.explain 不是函数”错误,因为它正在 mongodb shell 中工作并显示这些结果。

db.getCollection('users').explain("allPlansExecution").aggregate([
    {$match: {"categories.dotoris._id": ObjectId("5cbfbdd5a23e14127c603a1a")}},
    {$project:{_id:1, "categories._id":1, "categories.dotoris":1}}])
Run Code Online (Sandbox Code Playgroud)

结果 在此输入图像描述


在我搜索 mongoose 文档后,我发现我可以使用以下函数

User.aggregate([]).explain("executionStats")
Run Code Online (Sandbox Code Playgroud)

但它只有queryPlanner信息,但有executionStats。

如何通过猫鼬中的聚合获取executionStats信息?

rof*_*rol 0

在 mongoose-6.2 中有选项详细程度https://github.com/Automattic/mongoose/blob/6.2/lib/aggregate.js#L717

Aggregate.prototype.explain = function(verbosity, callback) {

无法使用executionStatsmongoose < 6.2 和aggreate https://github.com/Automattic/mongoose/blob/6.1/lib/aggregate.js#L703。但我们可以打开分析并从命令字段解析管道:

db.getProfilingStatus(); db.setProfilingLevel(0); db.system.profile.drop(); db.createCollection("system.profile", {capped: true, size:10485760 }); db.setProfilingLevel(2); db.getProfilingStatus();
var pipeline = [...];
var exp = db.getCollection('orders').explain('executionStats').aggregate(pipeline);
db.system.profile.find({"ns" : "shop.orders"})
print(JSON.stringify(exp));
// To have streamlined output:
// git clone https://github.com/gharriso/MongoDBPerformanceTuningBook
load('/path/to/MongoDBPerformanceTuningBook/mongoTuning.js');
mongoTuning.aggregationExecutionStats(exp);
Run Code Online (Sandbox Code Playgroud)