How to add allowDiskUse: true in spring mongo view creation

1k Views Asked by At

I am creating a view in Mongo Db in my Springboot application.Below is the code of same

        
[{
    $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

Stacktrace: |/ java.lang.Exception: [profile_event_view@stage [replica set: ]] Database error! |___/ 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.'

How can i add allowDiskUse: true in my code in order to avoid above error?

1

There are 1 best solutions below

0
On

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. You may also use "$$REMOVE" instead of separate $unset stage, e.g.

{
   "$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

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

Try to run it only once, reuse the result (i.e. data field) several times. Operation $setWindowFields may provide the same with less effort.

The same applies for 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" }
         ]
      }
   }
])