node.jsmongodbmongoosetypegoose

findByIdAndUpdate is not working with GridFs Object


I am trying to add one metadata to file object in Grid fs in mongo. But it is not updating (actually I am adding new property called page) metadata in Gridfs.

Following is my code which after saving of document, it tries to update that file with additional property.

fs.createReadStream(file)
          .pipe(bucket.openUploadStream(documentId.toString()))
          .on('finish', async (savedFile: File) => {
            let page = 1;

            const letssee = await FilesModel(
              projectContext,
              bucketName,
            ).findById(savedFile._id); // findById is working.

            await FilesModel(
              projectContext,
              bucketName,
            ).findByIdAndUpdate(savedFile._id, { $set: { page } }); // findByIdAndUpdate is not working.

          });

What wrong am I doing here? Or simply Grid Fs object doesn't support metadata update?


Solution

  • from the comments, the solution was:

    try adding .exec() after the findByIdAndUpdate query

    used like await Model.find().exec()