Convert Array to Object in JSON DB

Hi Everyone in this Great Community,

I am working on making a central DB from 30 files of database files most of them Json Files
I have made a schema and faced this 40M JSON Document with this Structure:

{"_id":{"$oid":"5fc8d0dfa8bf583ff047b691"},
"a":"humble, texas, united states",
"t":["1-619-549-5428","1-316-871-3930","1-801-953-3210","1-409-755-0267","1-314-989-0140","1-316-683-8794"],
"e":["iluv2flie@yahoo.com","billy.mckay@gmail.com"],
"liid":"billy-mckay-64217246",
"linkedin":"https://www.linkedin.com/in/billy-mckay-64217246",
"n":"billy mckay"}

Screenshot:

I want to modify all 4 Million Documents in the JSON Database to look like this:

{"_id":{"$oid":"5fc8d0dfa8bf583ff047b691"},
"a":"humble, texas, united states",
"t":{"phone1":"1-619-549-5428","phone2":"1-316-871-3930","phone3":"1-801-953-3210","phone4":"1-409-755-0267","1-314-989-0140","1-316-683-8794"},
"e":{"email1":"iluv2flie@yahoo.com","email2":"billy.mckay@gmail.com"},
"liid":"billy-mckay-64217246",
"linkedin":"https://www.linkedin.com/in/billy-mckay-64217246",
"n":"billy mckay"}

Thank you so much

Hello : )

Doc was

And became

Its replaces the t,e fields converting the arrays to objects.
It is $addFields all other fields remains the same.
You can run that pipeline below,in any driver.
Its the same code 2x it can wrapped in a function also,to reduce query size.

{
  "aggregate": "testcoll",
  "pipeline": [
    {
      "$addFields": {
        "t": {
          "$arrayToObject": {
            "$zip": {
              "inputs": [
                {
                  "$map": {
                    "input": {
                      "$range": [
                        1,
                        {
                          "$add": [
                            {
                              "$size": "$t"
                            },
                            1
                          ]
                        }
                      ]
                    },
                    "as": "n",
                    "in": {
                      "$concat": [
                        "phone",
                        {
                          "$toString": "$$n"
                        }
                      ]
                    }
                  }
                },
                {
                  "$map": {
                    "input": "$t",
                    "as": "m",
                    "in": {
                      "$trim": {
                        "input": "$$m"
                      }
                    }
                  }
                }
              ]
            }
          }
        }
      }
    },
    {
      "$addFields": {
        "e": {
          "$arrayToObject": {
            "$zip": {
              "inputs": [
                {
                  "$map": {
                    "input": {
                      "$range": [
                        1,
                        {
                          "$add": [
                            {
                              "$size": "$e"
                            },
                            1
                          ]
                        }
                      ]
                    },
                    "as": "n",
                    "in": {
                      "$concat": [
                        "email",
                        {
                          "$toString": "$$n"
                        }
                      ]
                    }
                  }
                },
                {
                  "$map": {
                    "input": "$e",
                    "as": "m",
                    "in": {
                      "$trim": {
                        "input": "$$m"
                      }
                    }
                  }
                }
              ]
            }
          }
        }
      }
    },
    {
      "$out": {
        "db": "testdb",
        "coll": "testcoll1"
      }
    }
  ],
  "cursor": {},
  "maxTimeMS": 1200000
}
1 Like