我如何优化此查询,$ ememMatch需要花费更多时间来执行

时间:2018-07-18 06:10:18

标签: mongodb mongoose mongodb-query aggregation-framework aggregate

我如何才能使执行更快,以下查询从55,000条记录中花费3.3秒执行时间,根据我的说法,$elemMatch需要花费更多时间,而没有这种条件的执行时间是毫秒。请帮助我找到提高速度的方法。

db.getCollection('collection').aggregate([{
        "$match": {
            "is_expired": false,
            "is_completed": true,
            "is_deleted": false,
            "$and": [{
                "$or": [{
                    "reported_abusive": {
                        "$ne": true
                    }
                }, {
                    "created_by": ObjectId("5ac6123e385e6a58669d8b06")
                }]
            }]
        }
    }, {
        "$sort": {
            "time_frame.end_on": 1
        }
    }, {
        "$lookup": {
            "from": "dburst",
            "localField": "_id",
            "foreignField": "dbursts.t_id",
            "as": "dburst"
        }
    }, {
        "$lookup": {
            "from": "sburst",
            "localField": "_id",
            "foreignField": "t_id",
            "as": "sburst"
        }
    }, {
        "$match": {
            "$or": [{
                "created_by": ObjectId("5ac6123e385e6a58669d8b06")
            }, {
                "sburst": {
                    "$elemMatch": {
                        "shared_with": {
                            "$elemMatch": {
                                "user_id": ObjectId("5ac6123e385e6a58669d8b06"),
                                "is_dbursted": false,
                                "is_deleted": false
                            }
                        }
                    }
                }
            }, {
                "dburst": {
                    "$elemMatch": {
                        "user_id": ObjectId("5ac6123e385e6a58669d8b06")
                    }
                }
            }]
        }
    }, {
        "$skip": 0
    }, {
        "$limit": 10
    }])

0 个答案:

没有答案