1

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"
            }],


1 Answer 1

1

You can use. It creates an array and deletes the older object.

db.getCollection('test2').update({},[{
    $set: {
            supplementalLessonVideos: {
                $concatArrays: [
                    [], //Creating new array
                    [{ //Adding values from object
                        videoId:"supplementalLessonVideo.videoId",
                        encoding:"supplementalLessonVideo.encoding"                    
                    }]
                ]
                }}          
},

{
    $unset:"supplementalLessonVideo"
}])

updateMany also works in this case.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.