Filtering & Aggregating data on the basis of a predefined Schema

Hi,

I’m trying to insert a document into a new collection by using a predefined schema via python
The Schema looks like this,

{
  "class" : " ",
  "Details" : [ 
{
   "name": " "
    "roll_no" : 0,
    "weekly_attendance" : [ ]
  } 
]
}

Screenshot from 2021-12-27 14-17-30

The Data that I’m trying to dump is :


{
  "class" : "X",
  "active": true,
  "Details" : [
  {
    "name" : "sam"
    "gender" : "Male",
    "roll_no" : 5,
    "Address" : "H-123, Block-4,601110",
    "weekly_attendance" : ["P","A","P","P","A"]
  }

  {
    "name" : "abc"
    "gender" : "Female",
    "Address" : "H-456, Block-6,601110",
    "weekly_attendance" : ["P","A","P","P","A"]
  }

  {
    "xyz" : "xyz"
    "roll_no" : 1,
    "Address" : "H-789, Block-10,601110",
  }
    ]
}

and the final result that I hope to achieve is to insert a document with the data as:

{
  "class" : "X",
  "Details" : [
     {
    "name" : "sam"
    "roll_no" : 5,
    "weekly_attendance" : ["P","A","P","P","A"]
  }

  {
    "name" : "abc"
    "roll_no" : 0,
    "weekly_attendance" : ["P","A","P","P","A"]
  }

  {
    "xyz" : "xyz"
    "roll_no" : 1,
    "weekly_attendance" : [ ]
  }
  ]
}

So I want to delete the additional fields from the data and in case of unavailability of data, I would like to have an empty placeholder just like in the schema and in the case of a nested document I want it to follow the same predefined schema for every new entry.

I tried dumping the schema in the collection first and then tried updating the same using $set, it worked fine in case of flattened out document which didn’t include any nested data but in the case of nested data the updated fails as it overwrites the whole data, and I wasn’t able to make the nested data follow the schema. Is there any solution for this problem, if yes then please help me out.

Thanks in advance.