Flatten and merge a set of mixed objects\arrays

I’m trying to flatten and merge a set of mixed objects\arrays but failed (
Sample:

{
 "main": "xxx",
 "phone_numbers": [
  "+1234567890",
  "+0987654321"
 ],
 "branch": [{
   "company": {
    "name": "c_name1",
    "head": "M.J.",
    "location": {
     "address": "address1",
     "phone": "phone1"
    },
    "url": "name1.com"
   },
   "title": {
    "name": "c_name1",
    "role": "marketing",
    "respons": [
     "marketing",
     "business administration"
    ]
   },
   "prev_location": [
    "loc_name1",
    "loc_name2"
   ],
   "summary": "summary1"
  },
  {
   "company": {
    "name": "c_name2",
    "head": "J.S.",
    "location": {
     "address": "address2",
     "phone": "phone1"
    },
    "url": "name2.com"
   },
   "title": {
    "name": "c_name2",
    "role": "HR",
    "respons": [
     "planning",
     "supervising the employment"
    ]
   },
   "location_names": [
    "loc_name3",
    "loc_name4"
   ],
   "summary": "summary2"
  }
 ]
}

Desired output:

{
"main": "xxx",
"phone_numbers": "+1234567890 , +0987654321",
"branch0-company": "name: c_name1; head: M.J.; address: address1; phone: phone1; url: name1.com",
"branch0-title": "name: c_name1; role: marketing; respons: marketing,business administration".
"branch0-prev_location": "loc_name1; loc_name2".
"branch0-summary": "summary1"
"branch1-company": "name: c_name2; head: J.S.; address: address2; phone: phone2; url: name1.com",
"branch1-title": "name: c_name1; role: marketing; respons: planning, supervising the employment ".
"branch1-prev_location": "loc_name3", "loc_name4".
"branch1-summary": "summary2"
}

The only thing I managed to do is flattening root array - Mongo playground
Any help or advice will be appreciated, thanks a lot.

Hi @V_V1 ,

Why won’t you write a client side function that parse a returned document this way.

Doing this logic inside a projection or aggregation will be over complex compare to the string parsing capabilities of the client side code :slight_smile:

Thanks
Pavel

2Pavel
It’s a part of transformation chain (MongoDB JSON → CSV input to full text search platform) .
This Is the Way (c) :slight_smile:

Is it a piece of code that does MongoDB Json → CSV?

We have atlas search allowing you to do full text search without the need of transformations or flattening…

2Pavel
Yes, sort of :slight_smile:
I know, but it is production platform, so there is no other way…