MongoDB slow query

finding the evil query that slows down mongodb and everything

Finding slow query with high Query Targeting

Finding slow query using log

{
    "t": {
        "$date": "2023-02-24T14:00:29.842+11:00"
    },
    "s": "I",
    "c": "COMMAND",
    "id": 51803,
    "ctx": "conn19",
    "msg": "Slow query",
    "attr": {
        "type": "command",
        "ns": "test.kv_event",
        "command": {
            "aggregate": "kv_event",
            "pipeline": [
                {
                    "$match": {
                        "key": "douyu_1144379",
                        "value": "online",
                        "_created_at": {
                            "$lt": {
                                "$date": "2023-02-24T03:00:28.527Z"
                            },
                            "$gte": {
                                "$date": "2023-02-23T21:00:28.527Z"
                            }
                        }
                    }
                },
                {
                    "$group": {
                        "_id": 1,
                        "n": {
                            "$sum": 1
                        }
                    }
                }
            ],
            "cursor": {},
            "lsid": {
                "id": {
                    "$uuid": "04a064ac-53fa-4b39-927b-2fee8453df2e"
                }
            },
            "$db": "test"
        },
        "planSummary": "COLLSCAN",
        "keysExamined": 0,
        "docsExamined": 1646884,
        "cursorExhausted": true,
        "numYields": 1646,
        "nreturned": 1,
        "queryHash": "54B8049A",
        "planCacheKey": "216AB424",
        "reslen": 126,
        "locks": {
            "FeatureCompatibilityVersion": {
                "acquireCount": {
                    "r": 1648
                }
            },
            "Global": {
                "acquireCount": {
                    "r": 1648
                }
            },
            "Mutex": {
                "acquireCount": {
                    "r": 2
                }
            }
        },
        "storage": {},
        "remote": "127.0.0.1:58222",
        "protocol": "op_msg",
        "durationMillis": 664
    }
}

After adding index