在 Mongo 中测量查询时间

Measure time of query in Mongo

如何测量 MongoDB 中查询的执行时间? 我找到了 Mongo-hacker 插件,但它看起来像是在测量查询时间,包括显示所有结果的时间。 在 PostgreSQL 中,我使用 Explain Analyze SELECT ... ,但我没有在 mongo 的 db.collection.find({smth}).explain()[=11 中找到任何关于时间的信息=]

最简单的方法是在 MongoDB 中设置分析级别:https://docs.mongodb.org/manual/tutorial/manage-the-database-profiler/

完成此操作后,所有查询的详细信息都将记录到系统配置文件中 table。

您可以在查询的末尾添加.explain("executionStats")

使用 Robo 3T,这在聚合中对我有用 -

db.getCollection('product_verification').explain("executionStats").aggregate([
{$match: {"company_id":1}},
{$lookup: {
    "from": "product",
    "let": {"company": "$company_id", "code": "$item_code"},
    "pipeline": [
       {$match: {$expr: {$and: [
           {"$eq": ["$company_id", "$$company"]},
           {"$eq": ["$item_code", "$$code"]},
           {"$in": ["$brand_uid",[13, 20]]}
           ]}
           }}
    ],
    "as": "details"
    }}])

并且使用时不聚合

db.getCollection('product').find({name: "lenovo"}).explain("executionStats")