简体   繁体   English

如何在 spring mongo 视图创建中添加 allowDiskUse: true

[英]How to add allowDiskUse: true in spring mongo view creation

I am creating a view in Mongo Db in my Springboot application.Below is the code of same我正在我的 Springboot 应用程序中的 Mongo Db 中创建一个视图。下面是相同的代码

        
[{
    $sort: {
        event_trigger_date: -1
    }
}, {
    $group: {
        _id: {
            "profile_id": "$profile_id"
        },
        data: {
            $first: "$$ROOT"
        }
    }
}, {
    $unset: "_id"
}, {
    $replaceRoot: {
        newRoot: "$data"
    }
}, {
    $project: {
        "profile_id": 1
    }
}, {
    $lookup: {
        from: 'profile_event',
        localField: 'profile_id',
        foreignField: 'profile_id',
        as: 'profile_event_data'
    }
}, {
    $group: {
        _id: {
            "profile_id": "$profile_id"
        },
        data: {
            $first: "$$ROOT"
        }
    }
}, {
    $replaceRoot: {
        newRoot: "$data"
    }
}, {
    $project: {
        profile_id: 1,
        profile_event_data: 1,
        event_type_set: {
            $concatArrays: ["$profile_event_data.event_type"]
        }
    }
}, {
    $addFields: {
        _id: {
            $concat: ["ACTIONS_NOT_COMPLETED_0X:", "$profile_id"]
        },
        event_type: "ACTIONS_NOT_COMPLETED_NX",
        event_trigger_date: "$$NOW",
        event_occurence: 0,
        trigger_status: "SILENT"
    }
}, {
    $unset: "event_exists"
}, {
    $lookup: {
        from: 'profile_personal_info',
        localField: 'profile_id',
        foreignField: 'profile_id',
        as: 'personal_info'
    }
}, {
    $project: {
        profile_id: 1,
        event_type: 1,
        event_trigger_date: 1,
        event_occurence: 1,
        trigger_status: 1,
        event_type_set: 1,
        personal_info: {
            $arrayElemAt: ["$personal_info", 0]
        }
    }
}, {
    $addFields: {
        oldest_personal_info_created_date: {
            $trunc: {
                $divide: [{
                    $subtract: ["$$NOW", '$personal_info.created_date']
                }, 1000 * 60 * 60 * 24]
            }
        }
    }
}, {
    $addFields: {
        created_date: {
            $trunc: {
                $divide: [{
                    $subtract: ["$$NOW", '$event_trigger_date']
                }, 1000 * 60 * 60 * 24]
            }
        }
    }
}, {
    $project: {
        event_type: 1,
        profile_id: 1,
        event_trigger_date: 1,
        profile_event_data: 1,
        event_type_set: 1,
        event_occurence: 1,
        trigger_status: 1,
        category_value: {
            $cond: {
                if: {
                    $eq: ["$oldest_personal_info_created_date", null]
                },
                then: "$created_date",
                else: "$oldest_personal_info_created_date"
            }
        }
    }
}, {
    $project: {
        profile_id: 1,
        event_type: 1,
        event_type_set: 1,
        event_trigger_date: 1,
        event_occurence: 1,
        trigger_status: 1,
        category_value: 1,
        "event_exists": {
            $in: ["ACTIONS_NOT_COMPLETED_NX", "$event_type_set"]
        }
    }
}, {
    $match: {
        event_exists: {
            $ne: true
        }
    }
}, {
    $unset: ["event_exists", "event_type_set"]
}]

I want to add allowDiskUse: true condition as i get following error我想添加 allowDiskUse: true 条件,因为出现以下错误

Stacktrace: |堆栈跟踪: | / java.lang.Exception: [profile_event_view@stage [replica set: ]] Database error! / java.lang.Exception:[profile_event_view@stage [副本集:]] 数据库错误! | | ___/ Mongo Server error (MongoQueryException): Query failed with error code 292 and error message 'PlanExecutor error during aggregation:: caused by:: Sort exceeded memory limit of 33554432 bytes, but did not opt in to external sorting.' ___/ Mongo 服务器错误 (MongoQueryException):查询失败,错误代码 292 和错误消息“聚合期间的 PlanExecutor 错误 :: 由 :: 排序超出 memory 限制 33554432 字节,但未选择外部排序。”

How can i add allowDiskUse: true in my code in order to avoid above error?如何在我的代码中添加 allowDiskUse: true 以避免上述错误?

Really a huge pipeline.真的是一条巨大的管道。 I think there is a lot room for optimizing.我觉得还有很大的优化空间。

For example if I an not mistake, these stages例如,如果我没记错的话,这些阶段

[
   {
      "$project": {
         "profile_id": 1,
         "event_type": 1,
         "event_trigger_date": 1,
         "event_occurence": 1,
         "trigger_status": 1,
         "oldest_lifebandinfo_created_date": 1,
         "profile_event_data": 1,
         "filteredList": { "$filter": { "input": "$profile_event_data", "as": "item", "cond": { "$eq": ["$$item.event_type", "ACTIONS_COMPLETED_DAILY"] } } }
      }
   },
   {
      "$project": {
         "profile_id": 1,
         "event_type": 1,
         "event_trigger_date": 1,
         "event_occurence": 1,
         "trigger_status": 1,
         "oldest_lifebandinfo_created_date": 1,
         "profile_event_data": 1,
         "filteredList": 1,
         "lastfilteredlist": { "$arrayElemAt": ["$filteredList", -1] }
      }
   },
   { "$unset": ["profile_event_data", "filteredList"] },
   {
      "$project": {
         "profile_id": 1,
         "event_type": 1,
         "event_trigger_date": 1,
         "event_occurence": 1,
         "trigger_status": 1,
         "oldest_lifebandinfo_created_date": 1,
         "latest_daily_action_creation_date": "$lastfilteredlist.created_date"
      }
   },
   { "$addFields": { "latest_daily_action_created_date": { "$trunc": { "$divide": [{ "$subtract": ["$$NOW", "$latest_daily_action_creation_date"] }, 86400000] } } } },
   {
      "$project": {
         "profile_id": 1,
         "event_type": 1,
         "event_trigger_date": 1,
         "event_occurence": 1,
         "trigger_status": 1,
         "oldest_lifebandinfo_created_date": 1,
         "latest_daily_action_created_date": 1,
         "category_value": { "$cond": { "if": { "$eq": ["$latest_daily_action_created_date", null] }, "then": "$oldest_lifebandinfo_created_date", "else": "$latest_daily_action_created_date" } }
      }
   },
   { "$unset": ["oldest_lifebandinfo_created_date", "latest_daily_action_created_date"] }]
]

you can simply write as below.你可以简单地写如下。 It does not make your aggregation pipeline faster but it will be easier to read:它不会使您的聚合管道更快,但会更容易阅读:

[
   {
      "$set": {
         "filteredList": { "$filter": { "input": "$profile_event_data", "as": "item", "cond": { "$eq": ["$$item.event_type", "ACTIONS_COMPLETED_DAILY"] } } }
      }
   },
   {
      "$project": {
         "latest_daily_action_created_date": { "$trunc": { "$divide": [{ "$subtract": ["$$NOW", { "$arrayElemAt": ["$filteredList.created_date", -1] }] }, 86400000] } },
         "profile_id": 1,
         "event_type": 1,
         "event_trigger_date": 1,
         "event_occurence": 1,
         "trigger_status": 1,
         "category_value": { "$cond": { "if": { "$eq": ["$latest_daily_action_created_date", null] }, "then": "$oldest_lifebandinfo_created_date", "else": "$latest_daily_action_created_date" } }
      }
   }
]

Note, every field which is not present in $project is removed.请注意, $project中不存在的每个字段都将被删除。 You may also use "$$REMOVE" instead of separate $unset stage, eg您也可以使用"$$REMOVE"代替单独的$unset阶段,例如

{
   "$addFields": {
      "event_trigger_date": "$lifeband_info.created_date",
      "filteredList": "$$REMOVE"
   }
}

Another example:另一个例子:

[
   { "$unset": "_id" },
   { "$replaceRoot": { "newRoot": "$data" } },
   { "$project": { "profile_id": 1 } },
]

becomes成为

[
   { "$project": { "profile_id": "$data.profile_id", _id: 0 } }
]

You run 12 times你跑了 12 次

{ "$group": { "_id": { "profile_id": "$profile_id" }, "data": { "$first": "$$ROOT" } } }

Try to run it only once, reuse the result (ie data field) several times.尝试只运行一次,多次重复使用结果(即data字段)。 Operation $setWindowFields may provide the same with less effort.操作$setWindowFields可能会以更少的努力提供相同的功能。

The same applies for 18(!) $lookup .这同样适用于 18(!) $lookup Join every collection only once and reuse the joined data.仅加入每个集合一次并重用加入的数据。

Reformatted pipeline:重新格式化的管道:

db.profile_event.aggregate([
   { "$sort": { "event_trigger_date": -1 } },
   { "$group": { "_id": { "profile_id": "$profile_id" }, "data": { "$first": "$$ROOT" } } },
   { "$unset": "_id" },
   { "$replaceRoot": { "newRoot": "$data" } },
   {
      "$project": {
         "profile_id": 1
      }
   },
   { "$lookup": { "from": "profile_event", "localField": "profile_id", "foreignField": "profile_id", "as": "profile_event_data" } },
   { "$group": { "_id": { "profile_id": "$profile_id" }, "data": { "$first": "$$ROOT" } } },
   { "$replaceRoot": { "newRoot": "$data" } },
   {
      "$project": {
         "profile_id": 1,
         "profile_event_data": 1,
         "event_type_set": { "$concatArrays": ["$profile_event_data.event_type"] }
      }
   },
   {
      "$addFields": {
         "_id": { "$concat": ["ACTIONS_NOT_COMPLETED_0X:", "$profile_id"] },
         "event_type": "ACTIONS_NOT_COMPLETED_NX",
         "event_trigger_date": "$$NOW",
         "event_occurence": 0,
         "trigger_status": "SILENT"
      }
   },
   { "$unset": "event_exists" },
   { "$lookup": { "from": "profile_lifeband_info", "localField": "profile_id", "foreignField": "profile_id", "as": "lifeband_info" } },
   {
      "$project": {
         "profile_id": 1,
         "event_type": 1,
         "event_trigger_date": 1,
         "event_occurence": 1,
         "trigger_status": 1,
         "event_type_set": 1,
         "lifeband_info": { "$arrayElemAt": ["$lifeband_info", 0] }
      }
   },
   { "$addFields": { "oldest_lifebandinfo_created_date": { "$trunc": { "$divide": [{ "$subtract": ["$$NOW", "$lifeband_info.created_date"] }, 86400000] } } } },
   { "$addFields": { "created_date": { "$trunc": { "$divide": [{ "$subtract": ["$$NOW", "$event_trigger_date"] }, 86400000] } } } },
   {
      "$project": {
         "event_type": 1,
         "profile_id": 1,
         "event_trigger_date": 1,
         "profile_event_data": 1,
         "event_type_set": 1,
         "event_occurence": 1,
         "trigger_status": 1,
         "category_value": { "$cond": { "if": { "$eq": ["$oldest_lifebandinfo_created_date", null] }, "then": "$created_date", "else": "$oldest_lifebandinfo_created_date" } }
      }
   },
   {
      "$project": {
         "profile_id": 1,
         "event_type": 1,
         "event_type_set": 1,
         "event_trigger_date": 1,
         "event_occurence": 1,
         "trigger_status": 1, "category_value": 1, "event_exists": { "$in": ["ACTIONS_NOT_COMPLETED_NX", "$event_type_set"] }
      }
   },
   { "$match": { "event_exists": { "$ne": true } } },
   { "$unset": ["event_exists", "event_type_set"] },
   {
      "$unionWith": {
         "coll": "profile_event", "pipeline": [
            { "$match": { "event_type": { "$eq": "ACTIONS_NOT_COMPLETED_NX" } } },
            { "$sort": { "event_trigger_date": -1 } },
            { "$group": { "_id": { "profile_id": "$profile_id" }, "data": { "$first": "$$ROOT" } } },
            { "$unset": "_id" },
            { "$replaceRoot": { "newRoot": "$data" } },
            { "$unset": ["last_modified_date", "notification_type", "progress_status"] },
            { "$lookup": { "from": "profile_lifeband_info", "localField": "profile_id", "foreignField": "profile_id", "as": "lifeband_info" } },
            { "$group": { "_id": { "profile_id": "$profile_id" }, "data": { "$first": "$$ROOT" } } },
            { "$replaceRoot": { "newRoot": "$data" } },
            {
               "$project": {
                  "profile_id": 1,
                  "event_type": 1,
                  "event_trigger_date": 1,
                  "event_occurence": 1,
                  "trigger_status": 1,
                  "filteredList": 1,
                  "lifeband_info": { "$arrayElemAt": ["$lifeband_info", 0] }
               }
            },
            { "$addFields": { "oldest_lifebandinfo_created_date": { "$trunc": { "$divide": [{ "$subtract": ["$$NOW", "$lifeband_info.created_date"] }, 86400000] } } } },
            { "$lookup": { "from": "profile_event", "localField": "profile_id", "foreignField": "profile_id", "as": "profile_event_data" } },
            {
               "$project": {
                  "profile_id": 1,
                  "event_type": 1,
                  "event_trigger_date": 1,
                  "event_occurence": 1,
                  "trigger_status": 1,
                  "oldest_lifebandinfo_created_date": 1,
                  "profile_event_data": 1,
                  "filteredList": { "$filter": { "input": "$profile_event_data", "as": "item", "cond": { "$eq": ["$$item.event_type", "ACTIONS_COMPLETED_DAILY"] } } }
               }
            },
            {
               "$project": {
                  "profile_id": 1,
                  "event_type": 1,
                  "event_trigger_date": 1,
                  "event_occurence": 1,
                  "trigger_status": 1,
                  "oldest_lifebandinfo_created_date": 1,
                  "profile_event_data": 1,
                  "filteredList": 1,
                  "lastfilteredlist": { "$arrayElemAt": ["$filteredList", -1] }
               }
            },
            { "$unset": ["profile_event_data", "filteredList"] },
            {
               "$project": {
                  "profile_id": 1,
                  "event_type": 1,
                  "event_trigger_date": 1,
                  "event_occurence": 1,
                  "trigger_status": 1,
                  "oldest_lifebandinfo_created_date": 1,
                  "latest_daily_action_creation_date": "$lastfilteredlist.created_date"
               }
            },
            { "$addFields": { "latest_daily_action_created_date": { "$trunc": { "$divide": [{ "$subtract": ["$$NOW", "$latest_daily_action_creation_date"] }, 86400000] } } } },
            {
               "$project": {
                  "profile_id": 1,
                  "event_type": 1,
                  "event_trigger_date": 1,
                  "event_occurence": 1,
                  "trigger_status": 1,
                  "oldest_lifebandinfo_created_date": 1,
                  "latest_daily_action_created_date": 1,
                  "category_value": { "$cond": { "if": { "$eq": ["$latest_daily_action_created_date", null] }, "then": "$oldest_lifebandinfo_created_date", "else": "$latest_daily_action_created_date" } }
               }
            },
            { "$unset": ["oldest_lifebandinfo_created_date", "latest_daily_action_created_date"] }]
      }
   },
   {
      "$unionWith": {
         "coll": "profile_event", "pipeline": [
            { "$sort": { "event_trigger_date": -1 } },
            { "$group": { "_id": { "profile_id": "$profile_id" }, "data": { "$first": "$$ROOT" } } },
            { "$unset": "_id" },
            { "$replaceRoot": { "newRoot": "$data" } },
            { "$project": { "profile_id": 1 } },
            { "$lookup": { "from": "profile_event", "localField": "profile_id", "foreignField": "profile_id", "as": "profile_event_data" } },
            {
               "$project": {
                  "profile_id": 1,
                  "event_type_set": { "$concatArrays": ["$profile_event_data.event_type"] }
               }
            },
            {
               "$project": {
                  "profile_id": 1,
                  "event_exists": { "$in": ["HEALTH_FACTORS_NX", "$event_type_set"] }
               }
            },
            { "$match": { "event_exists": { "$ne": true } } },
            {
               "$addFields": {
                  "_id": { "$concat": ["HEALTH_FACTORS_0X:", "$profile_id"] },
                  "event_type": "HEALTH_FACTORS_NX",
                  "event_occurence": 0,
                  "trigger_status": "SILENT"
               }
            },
            { "$unset": "event_exists" },
            {
               "$addFields": {
                  "lifeband_event_id": { "$concat": ["lifeband:", "$profile_id"] }
               }
            },
            { "$lookup": { "from": "profile_event", "localField": "lifeband_event_id", "foreignField": "_id", "as": "lifeband" } },
            {
               "$project": {
                  "event_type": 1,
                  "profile_id": 1,
                  "event_occurence": 1,
                  "lifeband": { "$arrayElemAt": ["$lifeband", 0] },
                  "trigger_status": 1
               }
            },
            {
               "$addFields": {
                  "lifeband_last_modified_date": "$lifeband.last_modified_date",
                  "lifeband_last_modified_days": { "$trunc": { "$divide": [{ "$subtract": ["$$NOW", "$lifeband.last_modified_date"] }, 86400000] } }
               }
            },
            { "$lookup": { "from": "profile_lifeband_info", "localField": "profile_id", "foreignField": "profile_id", "as": "lifeband_info" } },
            {
               "$project": {
                  "event_type": 1,
                  "profile_id": 1,
                  "event_trigger_date": 1,
                  "event_occurence": 1,
                  "lifeband_last_modified_days": 1,
                  "lifeband_last_modified_date": 1,
                  "lifeband": 1,
                  "trigger_status": 1, "lifeband_info": { "$arrayElemAt": ["$lifeband_info", 0] }
               }
            },
            {
               "$addFields": {
                  "lifeband_info_event_trigger_date": "$lifeband_info.created_date",
                  "lifeband_info_event_trigger_days": { "$trunc": { "$divide": [{ "$subtract": ["$$NOW", "$lifeband_info.created_date"] }, 86400000] } }
               }
            },
            {
               "$project": {
                  "event_type": 1,
                  "profile_id": 1,
                  "event_trigger_date": { "$cond": { "if": { "$eq": ["$lifeband_last_modified_date", null] }, "then": "$lifeband_info_event_trigger_date", "else": "$lifeband_last_modified_date" } },
                  "event_occurence": 1,
                  "trigger_status": 1,
                  "lifeband_info_event_trigger_date": 1,
                  "lifeband_info_event_trigger_days": 1,
                  "lifeband_last_modified_days": 1,
                  "lifeband_last_modified_date": 1,
                  "category_value": { "$cond": { "if": { "$eq": ["$lifeband_last_modified_days", null] }, "then": "$lifeband_info_event_trigger_days", "else": "$lifeband_last_modified_days" } }
               }
            },
            { "$unset": ["lifeband_last_modified_days", "lifeband_info_event_trigger_days", "lifeband_last_modified_date", "lifeband_info_event_trigger_date"] }]
      }
   },
   {
      "$unionWith": {
         "coll": "profile_event", "pipeline": [
            { "$match": { "event_type": { "$eq": "HEALTH_FACTORS_NX" } } },
            { "$sort": { "event_trigger_date": -1 } },
            { "$group": { "_id": { "profile_id": "$profile_id" }, "data": { "$first": "$$ROOT" } } },
            { "$unset": "_id" },
            { "$replaceRoot": { "newRoot": "$data" } },
            {
               "$addFields": {
                  "category_value": { "$trunc": { "$divide": [{ "$subtract": ["$$NOW", "$event_trigger_date"] }, 86400000] } }
               }
            },
            { "$unset": ["last_modified_date", "notification_type", "progress_status"] }]
      }
   },
   {
      "$unionWith": {
         "coll": "profile_event", "pipeline": [
            { "$sort": { "event_trigger_date": -1 } },
            { "$group": { "_id": { "profile_id": "$profile_id" }, "data": { "$first": "$$ROOT" } } },
            { "$unset": "_id" },
            { "$replaceRoot": { "newRoot": "$data" } },
            { "$lookup": { "from": "profile_lifeband_info", "localField": "profile_id", "foreignField": "profile_id", "as": "lifeband_info" } },
            {
               "$project": {
                  "profile_id": 1,
                  "lifeband_info": { "$arrayElemAt": ["$lifeband_info", 0] }
               }
            },
            { "$addFields": { "category_value": { "$abs": { "$subtract": [{ "$toDouble": "$lifeband_info.output.lifeband.accuracyLevel.max" }, { "$toDouble": "$lifeband_info.output.lifeband.accuracyLevel.min" }] } } } },
            {
               "$project": {
                  "profile_id": 1,
                  "category_value": 1
               }
            },
            { "$lookup": { "from": "profile_event", "localField": "profile_id", "foreignField": "profile_id", "as": "profile_event_data" } },
            {
               "$project": {
                  "profile_id": 1,
                  "category_value": 1,
                  "event_type_set": { "$concatArrays": ["$profile_event_data.event_type"] }
               }
            },
            {
               "$project": {
                  "profile_id": 1,
                  "category_value": 1,
                  "event_exists": { "$in": ["JUVBAND_ACCURACY_LOW_NX", "$event_type_set"] }
               }
            },
            { "$match": { "event_exists": { "$ne": true } } },
            {
               "$addFields": {
                  "_id": { "$concat": ["JUVBAND_ACCURACY_LOW_0X:", "$profile_id"] },
                  "event_type": "JUVBAND_ACCURACY_LOW_NX",
                  "event_trigger_date": "$$NOW",
                  "event_occurence": 0,
                  "trigger_status": "SILENT"
               }
            },
            { "$unset": "event_exists" }]
      }
   },
   {
      "$unionWith": {
         "coll": "profile_event", "pipeline": [
            { "$match": { "event_type": { "$eq": "JUVBAND_ACCURACY_LOW_NX" } } },
            { "$sort": { "event_trigger_date": -1 } },
            { "$group": { "_id": { "profile_id": "$profile_id" }, "data": { "$first": "$$ROOT" } } },
            { "$unset": "_id" },
            { "$replaceRoot": { "newRoot": "$data" } },
            { "$lookup": { "from": "profile", "localField": "profile_id", "foreignField": "_id", "as": "profile_data" } },
            { "$unwind": { "path": "$profile_data" } },
            { "$lookup": { "from": "profile_lifeband_info", "localField": "profile_data.profile_recommendation_info_id", "foreignField": "_id", "as": "lifeband_info" } },
            { "$unwind": { "path": "$lifeband_info" } },
            { "$addFields": { "category_value": { "$abs": { "$subtract": [{ "$toDouble": "$lifeband_info.output.lifeband.accuracyLevel.max" }, { "$toDouble": "$lifeband_info.output.lifeband.accuracyLevel.min" }] } } } },
            { "$unset": ["last_modified_data", "notification_type", "progress_status", "lifeband_info", "profile_data"] }]
      }
   },
   {
      "$unionWith": {
         "coll": "profile_event", "pipeline": [
            { "$sort": { "event_trigger_date": -1 } },
            { "$group": { "_id": { "profile_id": "$profile_id" }, "data": { "$first": "$$ROOT" } } },
            { "$unset": "_id" }, { "$replaceRoot": { "newRoot": "$data" } },
            { "$lookup": { "from": "profile_lifeband_info", "localField": "profile_id", "foreignField": "profile_id", "as": "lifeband_info" } },
            {
               "$project": {
                  "profile_id": 1,
                  "lifeband_info": { "$arrayElemAt": ["$lifeband_info", 0] }
               }
            },
            { "$addFields": { "category_value": { "$abs": { "$subtract": [{ "$toDouble": "$lifeband_info.output.lifeband.accuracyLevel.max" }, { "$toDouble": "$lifeband_info.output.lifeband.accuracyLevel.min" }] } } } },
            {
               "$project": {
                  "profile_id": 1,
                  "category_value": 1
               }
            },
            { "$lookup": { "from": "profile_event", "localField": "profile_id", "foreignField": "profile_id", "as": "profile_event_data" } },
            {
               "$project": {
                  "profile_id": 1, "category_value": 1,
                  "event_type_set": { "$concatArrays": ["$profile_event_data.event_type"] }
               }
            },
            {
               "$project": {
                  "profile_id": 1, "category_value": 1,
                  "event_exists": { "$in": ["JUVBAND_ACCURACY_MEDIUM_NX", "$event_type_set"] }
               }
            },
            { "$match": { "event_exists": { "$ne": true } } },
            {
               "$addFields": {
                  "_id": { "$concat": ["JUVBAND_ACCURACY_MEDIUM_0X:", "$profile_id"] },
                  "event_type": "JUVBAND_ACCURACY_MEDIUM_NX", "event_trigger_date": "$$NOW", "event_occurence": 0, "trigger_status": "SILENT"
               }
            },
            { "$unset": "event_exists" }]
      }
   },
   {
      "$unionWith": {
         "coll": "profile_event", "pipeline": [
            { "$match": { "event_type": { "$eq": "JUVBAND_ACCURACY_MEDIUM_NX" } } },
            { "$sort": { "event_trigger_date": -1 } }, 
            { "$group": { "_id": { "profile_id": "$profile_id" }, "data": { "$first": "$$ROOT" } } },
            { "$unset": "_id" }, { "$replaceRoot": { "newRoot": "$data" } },
            { "$lookup": { "from": "profile", "localField": "profile_id", "foreignField": "_id", "as": "profile_data" } },
            { "$unwind": { "path": "$profile_data" } },
            { "$lookup": { "from": "profile_lifeband_info", "localField": "profile_data.profile_recommendation_info_id", "foreignField": "_id", "as": "lifeband_info" } },
            { "$unwind": { "path": "$lifeband_info" } },
            {
               "$addFields": {
                  "category_value": { "$abs": { "$subtract": [{ "$toDouble": "$lifeband_info.output.lifeband.accuracyLevel.max" }, { "$toDouble": "$lifeband_info.output.lifeband.accuracyLevel.min" }] } }
               }
            },
            { "$unset": ["last_modified_date", "notification_type", "progress_status", "lifeband_info", "profile_data", "accuracy_level"] }
         ]
      }
   },
   {
      "$unionWith": {
         "coll": "profile_event", "pipeline": [
            { "$sort": { "event_trigger_date": -1 } },
            { "$group": { "_id": { "profile_id": "$profile_id" }, "data": { "$first": "$$ROOT" } } },
            { "$unset": "_id" }, { "$replaceRoot": { "newRoot": "$data" } },
            { "$addFields": { "profile_strength_id": { "$concat": ["profile_strength:", "$profile_id"] } } },
            { "$lookup": { "from": "profile_event", "localField": "profile_strength_id", "foreignField": "_id", "as": "profile_strength_info" } },
            { "$unwind": { "path": "$profile_strength_info" } }, { "$addFields": { "category_value": { "$toInt": "$profile_strength_info.progress_status" } } },
            { "$project": { "profile_id": 1, "category_value": 1 } },
            { "$lookup": { "from": "profile_event", "localField": "profile_id", "foreignField": "profile_id", "as": "profile_event_data" } },
            {
               "$project": {
                  "profile_id": 1,
                  "category_value": 1,
                  "event_type_set": { "$concatArrays": ["$profile_event_data.event_type"] }
               }
            },
            {
               "$project": {
                  "profile_id": 1,
                  "category_value": 1,
                  "event_exists": { "$in": ["PROFILE_MEDIUM_NX", "$event_type_set"] }
               }
            },
            { "$match": { "event_exists": { "$ne": true } } },
            {
               "$addFields": {
                  "_id": { "$concat": ["PROFILE_MEDIUM_0X:", "$profile_id"] },
                  "event_type": "PROFILE_MEDIUM_NX",
                  "event_occurence": 0,
                  "trigger_status": "SILENT"
               }
            },
            { "$unset": "event_exists" },
            {
               "$project": {
                  "event_type": 1,
                  "profile_id": 1,
                  "event_trigger_date": 1,
                  "event_occurence": 1,
                  "trigger_status": 1,
                  "category_value": { "$cond": { "if": { "$eq": ["$category_value", null] }, "then": 0, "else": "$category_value" } }
               }
            },
            { "$lookup": { "from": "profile_lifeband_info", "localField": "profile_id", "foreignField": "profile_id", "as": "lifeband_info" } },
            {
               "$project": {
                  "event_type": 1,
                  "profile_id": 1,
                  "event_trigger_date": 1,
                  "event_occurence": 1,
                  "lifeband": 1,
                  "trigger_status": 1,
                  "category_value": 1,
                  "lifeband_info": { "$arrayElemAt": ["$lifeband_info", 0] }
               }
            },
            {
               "$addFields": {
                  "event_trigger_date": "$lifeband_info.created_date"
               }
            },
            { "$unset": "lifeband_info" }
         ]
      }
   }
])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM