I'm trying to write a script to add a new field in a double nested array without deleting the entire array.
I'm trying to add a new field totalStudents to School.teacher.class,
This is the structure:
Work: [...],
School: [{
name: {type: String, default: ''},
teacher:
[{
class: {
enabled: {type: Boolean, default: true},
grade: [{type: String, default: ''}],
},
test: {
enabled: {type: Boolean, default: false},
duration: {type: Number, default: 5},
},
score: {type: Number, default: 0},
}]
curve:
[{
lower: {type: Number, default: 0},
upper: {type: Number, default: 0},
withinBoundary: {type: Boolean, default: false},
outsideBoundary: {type: Boolean, default: false},
enabled: {type: Boolean, default: true}
}]
}],
The code that I wrote:
db.colllection.updateMany({}, {$set: {School: [{teacher:[{class:{totalStudents: null}}]}]}})