Как сделать конвейер сложной агрегации легче

#mongodb

#mongodb

Вопрос:

У меня есть требование объединить 3-4 структуры данных в 1 в определенном формате. Я создал конвейер агрегации, но по какой-то причине он работает медленно.

сооружения

страница чертежа

 {  "_id" : 599,  "settings" : {  "title" : "New structure",  "description" : "",  "allow_search_engines" : false,  "keywords" : "",  "url" : "new-structure",  "seo_title" : "New structure"  },  "status" : 1,  "slug" : "new-structure",  "is_home_page" : false,  "title" : "New structure",  "theme_slug" : "kingdom" }  

draft_sections

 /* 1 */ {  "_id" : 3623,  "page_id" : 599,  "slug" : "thank_you",  "identifier" : "sec_123",  "elements" : {  "title" : "elem_abc",  "sub_title" : "elem_def",  "media" : "elem_ghi",  "button" : "elem_jkl",  "give_us_call" : "elem_mno"  },  "added_on" : NumberLong(1635145906) }  /* 2 */ {  "_id" : 3624,  "page_id" : 599,  "slug" : "banner",  "identifier" : "sec_123a",  "elements" : {  "title" : "elem_abca",  "sub_title" : "elem_defa",  "media" : "elem_ghia",  "button" : "elem_jkla",  "give_us_call" : "elem_mnoa"  },  "added_on" : NumberLong(1635145906) }  

draft_section_settings

  {  "_id" : ObjectId(""),  "identifier" : "elem_ghia",  "section_id" : 3624.0,  "settings" : {  "type" : "media",  "status" : true,  "mobile_status" : true,  "urls" : [   "/assets/banner-bg.jpg"  ]  } }  {  "_id" : ObjectId(""),  "identifier" : "elem_jkla",  "section_id" : 3624.0,  "settings" : {  "type" : "button",  "status" : true,  "mobile_status" : true,  "buttons" : [   "btn_123"  ]  } }  {  "_id" : ObjectId(""),  "identifier" : "elem_mnoa",  "section_id" : 3624.0,  "settings" : {  "type" : "group",  "status" : true,  "mobile_status" : true,  "added_elements" : [   "title",   "number"  ],  "elements" : {  "title" : "elem_001",  "number" : "elem_002"  }  } }  

draft_section_content

  {  "_id" : ObjectId(""),  "section_id" : 3623,  "page_id" : 599,  "content" : {  "elem_abc" : "abc",  "elem_def" : "Let us connect",  "btn_123" : "abc",  "elem_001" : "Give Us A Call:",  "elem_002" : " 01 123 456 7890"  },  "language_code" : "en" } {  "_id" : ObjectId(""),  "section_id" : 3624,  "page_id" : 599,  "content" : {  "sec_123a" : "abc",  "elem_defa" : "Let us connect",  "btn_123a" : "abc",  "elem_001a" : "Give Us A Call:",  "elem_002a" : " 01 123 456 7890"  },  "language_code" : "en" }  

Expected output:

 {  "_id": 599,  "added_sections": [  "sec_123a",  "sec_123"  ],  "content": {  "btn_123": "abc",  "btn_123a": "abc",  "elem_001": "Give Us A Call:",  "elem_001a": "Give Us A Call:",  "elem_002": " 01 123 456 7890",  "elem_002a": " 01 123 456 7890",  "elem_abc": "abc",  "elem_def": "Let us connect",  "elem_defa": "Let us connect",  "sec_123a": "abc"  },  "form_id": null,  "industry_id": null,  "is_home_page": false,  "page_settings": {  "allow_search_engines": false,  "description": "",  "keywords": "",  "seo_title": "New structure",  "title": "New structure",  "url": "new-structure"  },  "sections": {  "sec_123": {  "button": "elem_jkl",  "give_us_call": "elem_mno",  "media": "elem_ghi",  "sub_title": "elem_def",  "title": "elem_abc"  },  "sec_123a": {  "button": "elem_jkla",  "give_us_call": "elem_mnoa",  "media": "elem_ghia",  "sub_title": "elem_defa",  "title": "elem_abca"  }  },  "settings": {  "elem_abca": {  "mobile_status": true,  "status": true,  "type": "text"  },  "elem_defa": {  "mobile_status": true,  "status": true,  "type": "text"  },  "elem_ghia": {  "mobile_status": true,  "status": true,  "type": "media",  "urls": [  "/assets/banner-bg.jpg"  ]  },  "elem_jkla": {  "buttons": [  "btn_123"  ],  "mobile_status": true,  "status": true,  "type": "button"  },  "elem_mnoa": {  "added_elements": [  "title",  "number"  ],  "elements": {  "number": "elem_002",  "title": "elem_001"  },  "mobile_status": true,  "status": true,  "type": "group"  },  "sec_123": {  "mobile_status": true,  "status": true  },  "sec_123a": {  "mobile_status": true,  "slug": "banner",  "status": true,  "type": "section",  "variation_id": "bk_banner_V1"  }  },  "slug": "new-structure",  "status": 1,  "theme_slug": "kingdom",  "title": "New structure"  }  

I have used following pipeline to build this structure:

 db.getCollection('draft_pages').aggregate([  {$match: {  "_id": 599  }  },  {  $lookup: {  from: 'draft_sections',  localField: '_id',  foreignField: 'page_id',  as: 'sections'  }  },  {$unwind: {  path: "$sections",  preserveNullAndEmptyArrays: true  }  },  {  $lookup: {  from: "draft_section_content",  let: {  "id": "$sections._id"  },  pipeline: [  {  "$match": {  "$expr": {  "$eq": [  "$id",  "$section_id"  ]  },  "language_code": "en"  }  }  ],  as: "content"  }  },  {$unwind: {  path: "$content",  preserveNullAndEmptyArrays: true  }  },  {$addFields: {  "sections.elements_obj": {  $arrayToObject: [  [  {  "k": "$sections.identifier",  "v": "$sections.elements"  }  ]  ]  }  }  },  {$lookup: {  from: 'draft_section_settings',  localField: 'sections._id',  foreignField: 'section_id',  as: 'sec_settings'  }  },  {$unwind: {  path: "$sec_settings",  preserveNullAndEmptyArrays: true  }  },  {$addFields: {  "sec_settings": {  $arrayToObject: [  [  {  "k": "$sec_settings.identifier",  "v": "$sec_settings.settings"  }  ]  ]  }  }  },  {$group: {  "_id": "$_id",  "industry_id": {  "$first": "$industry_id"  },  "form_id": {  "$first": "$form_id"  },  "page_settings": {  "$first": "$settings"  },  "status": {  "$first": "$status"  },  "slug": {  "$first": "$slug"  },  "is_home_page": {  "$first": "$is_home_page"  },  "title": {  "$first": "$title"  },  "theme_slug": {  "$first": "$theme_slug"  },  "added_sections": {  $addToSet: "$sections.identifier"  },  "sections": {  $mergeObjects: "$sections.elements_obj"  },  "settings": {  $mergeObjects: "$sec_settings"  },  "content": {  $mergeObjects: "$content.content"  }  }  } ])  

Как я могу облегчить этот конвейер с помощью существующей структуры данных ?