0

I have a document with this structure:

{
  "_id": {
    "$oid": "639a1d134338565900901bc2"
  },
  "eventId": 6,
  "userId": {
    "$oid": "636d3ffb7d4cf8067dedc5d4"
  },
  "createdAt": {
    "$date": {
      "$numberLong": "1671044371692"
    }
  },
  "lastModified": {
    "$date": {
      "$numberLong": "1671132870160"
    }
  },
  "type": "Feature",
  "geometry": {
    "coordinates": [
      -71.820159,
      41.877574
    ],
    "type": "Point"
  },
  "properties": {
    "timestamp": {
      "$date": {
        "$numberLong": "1671044350325"
      }
    },
    "forms": [
      {
        "formId": 14,
        "field0": "CFS1 Fire",
        "field1": "Dispatched",
        "field2": null,
        "_id": {
          "$oid": "639a1d134338565900901bc3"
        }
      },
      {
        "formId": 13,
        "field1": "test",
        "_id": {
          "$oid": "639b76c64338565900901c42"
        }
      }
    ]
  },
  "favoriteUserIds": [],
  "states": [
    {
      "name": "active",
      "userId": {
        "$oid": "636d3ffb7d4cf8067dedc5d4"
      },
      "_id": {
        "$oid": "639a1d134338565900901bc4"
      }
    }
  ],
  "attachments": [],
  "__v": 1
}

I am working on mongodb compass. I want to create a new view where when I call the above document, the fields nested inside the "properties" field are at the top level. Meaning that the above document would turn to this:

{
  "_id": {
    "$oid": "639a1d134338565900901bc2"
  },
  "eventId": 6,
  "userId": {
    "$oid": "636d3ffb7d4cf8067dedc5d4"
  },
  "createdAt": {
    "$date": {
      "$numberLong": "1671044371692"
    }
  },
  "lastModified": {
    "$date": {
      "$numberLong": "1671132870160"
    }
  },
  "type": "Feature",
  "geometry": {
    "coordinates": [
      -71.820159,
      41.877574
    ],
    "type": "Point"
  },
  "formId1": 14,
  "form_1_field0": "CFS1 Fire",
  "form_1_field1": "Dispatched",
  "form_1_field2": null,
  "_id": {"$oid": "639a1d134338565900901bc3"},

  "formId2": 13,
  "form_2_field1": "test",
  "_id": {"$oid": "639b76c64338565900901c42"}

  "properties": {
    "timestamp": {
      "$date": {
        "$numberLong": "1671044350325"
      }
    },
  },
  "favoriteUserIds": [],
  "states": [
    {
      "name": "active",
      "userId": {
        "$oid": "636d3ffb7d4cf8067dedc5d4"
      },
      "_id": {
        "$oid": "639a1d134338565900901bc4"
      }
    }
  ],
  "attachments": [],
  "__v": 1
}

I want to do this only with the "properties" field. Also, there may be instances where the "forms" array inside the "properties" field is empty. Also, each object has different number of fields.

I'm new to mongodb and therefor new to mongodb compass. I suspect I can do this by using aggregations in compass, but not sure if I compass is what I am supposed to be using or something else.

Angel Ortiz
  • 72
  • 13
  • 1
    the expected output has duplicate field names, and its not a valid json document, you have to either change the names, or keep them in embeded document or array as they were, because you cant have same field names in same level – Takis Dec 18 '22 at 15:45
  • @Takis. I apologize. I have made the edit. Suppose the naming convention for formIdi, and form_i_fieldx, where i is the index of the form starting from one (1) and the x is the field, starting from zero (0). – Angel Ortiz Dec 18 '22 at 23:43

0 Answers0