I have an array of lessons that contain supplementalLessonVideo, which is an object. I want to now make the field instead be an array called supplementalLessonVideos and move the object from supplementalLessonVideo into the first index with a query. Not all lessons contain the field either, only some.
Schema:
...
lessons: [
{
supplementalLessonVideo:
{
videoId: { type: Schema.Types.ObjectId, ref: "Video" },
_id: { type: Schema.Types.ObjectId },
fieldname: { type: String },
originalname: { type: String },
encoding: { type: String },
mimetype: { type: String },
filename: { type: String },
size: { type: Number },
key: { type: String },
url: { type: String },
},
},
];
and I am wanting to make it into:
...
lessons:[
{
supplementalLessonVideos:
[
{
videoId: { type: Schema.Types.ObjectId, ref: "Video" },
_id: { type: Schema.Types.ObjectId },
fieldname: { type: String },
originalname: { type: String },
encoding: { type: String },
mimetype: { type: String },
filename: { type: String },
size: { type: Number },
key: { type: String },
url: { type: String },
}
]
}
]
Is there a query I can write and run on the DB to convert the field to an array and then move the object data into the first index of the array?
So, going from this:
"supplementalLessonVideo" : {
"videoId" : ObjectId("5ed868ab7c77482170d135f3"),
"_id" : ObjectId("5ed868ab7c77482170d135f4"),
"fieldname" : "supplemental-lesson-video",
"originalname" : "Student Navigation Video April 2018.mp4",
"encoding" : "7bit",
"mimetype" : "video/mp4",
"filename" : "supplemental-lesson-video-1591240848919-741719000",
"size" : 15255388,
"key" : "supplemental-lesson-video",
"url" : "https://vimeo.com"
},
To this:
"supplementalLessonVideos" : [{
"videoId" : ObjectId("5ed868ab7c77482170d135f3"),
"_id" : ObjectId("5ed868ab7c77482170d135f4"),
"fieldname" : "supplemental-lesson-video",
"originalname" : "Student Navigation Video April 2018.mp4",
"encoding" : "7bit",
"mimetype" : "video/mp4",
"filename" : "supplemental-lesson-video-1591240848919-741719000",
"size" : 15255388,
"key" : "supplemental-lesson-video",
"url" : "https://vimeo.com"
}],