'Mongodb: attaching the column to every array and object from a MongoDB and create csv using pymongo

I am still learning Mongo how to extract the data and load to relational database. I am trying to extract one of the column and add that to csv for all the array and object fields from MongoDB. Below example

{{
"_id":"69ajdslsdfdksjfef9",
"col1":"456780",
"refNum":"ref001"
}
{
  "clients":{
             "_id":"6789dsfsdkjfhsde8"
              "CLI_1": "9876547390",
              "CLI_2": "fsdfasl"
}
{"names":[
{
  "first":"dfsakfj",
  "middle":"hgfgas",
  "last":"komdssdfsd"
},
{
  "first":"dfskdajf",
  "middle": "fgjfgjfl",
  "last": "ghfghsdklfg"
}]}

second row from collection

{{
"_id":"69ajdslsdfdksjfef9",
"col1":"456780",
"refNum":"ref001"
}
{
  "clients":{
              "_id":"53545852pasdkfjd"
              "CLI_1": "9876547390",
              "CLI_2": "fsdfasl"
}
{"names":[
{
  "first":"dfsakfj",
  "middle":"hgfgas",
  "last":"komdssdfsd"
}]}

I am creating two csv files refnum.csv and clients.csv. How do I add refNum to clients.csv file to load both the files in to relational database where refNum would be the key for joining to extract other columns.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source