[ { "$match": { "_id": "664e4b56df183b06a20e2a23" } }, { "$addFields": { "id": { "$toString": "$_id" } } }, { "$lookup": { "from": "owners", "let": { "id": "$id" }, "pipeline": [ { "$match": { "$expr": { "$eq": [ "$agency", "$$id" ] }, "delete_flag": { "$ne": true }, "archived": { "$ne": true }, "$and": [ { "$or": [ { "full_name": { "$regex": ".*.*", "$options": "i" } }, { "legal_name": { "$regex": ".*.*", "$options": "i" } } ] } ], "_id": "678f925559b423d94d011dfa" } }, { "$limit": 1000 } ], "as": "owners" } }, { "$addFields": { "owners": { "$map": { "input": "$owners", "as": "owners", "in": { "$mergeObjects": [ "$$owners", { "model": "owners" } ] } } } } }, { "$lookup": { "from": "accounts", "let": { "id": "$id" }, "pipeline": [ { "$match": { "$expr": { "$eq": [ "$agency", "$$id" ] }, "delete_flag": { "$ne": true }, "archived": { "$ne": true }, "$and": [ { "$or": [ { "full_name": { "$regex": ".*.*", "$options": "i" } }, { "legal_name": { "$regex": ".*.*", "$options": "i" } } ] } ], "_id": "678f925559b423d94d011dfa" } }, { "$limit": 1000 } ], "as": "accounts" } }, { "$addFields": { "accounts": { "$map": { "input": "$accounts", "as": "accounts", "in": { "$mergeObjects": [ "$$accounts", { "model": "accounts" } ] } } } } }, { "$lookup": { "from": "companies", "let": { "id": "$id" }, "pipeline": [ { "$match": { "$expr": { "$eq": [ "$agency", "$$id" ] }, "delete_flag": { "$ne": true }, "archived": { "$ne": true }, "$and": [ { "$or": [ { "full_name": { "$regex": ".*.*", "$options": "i" } }, { "legal_name": { "$regex": ".*.*", "$options": "i" } } ] } ], "_id": "678f925559b423d94d011dfa" } }, { "$limit": 1000 } ], "as": "companies" } }, { "$addFields": { "companies": { "$map": { "input": "$companies", "as": "companies", "in": { "$mergeObjects": [ "$$companies", { "model": "companies" } ] } } } } }, { "$project": { "items": { "$concatArrays": [ "$owners", "$accounts", "$companies" ] } } } ]