обновление объекта в массиве из схемы mon goose - PullRequest
0 голосов
/ 16 марта 2020

Я создал операцию проекта crud в mongodb, все работает хорошо в операции обновления, мне нужно обновить объект, который находится внутри массива, который, как разные ID, может у кого-то есть решение

Пн goose Схема

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const educationSchema = new Schema({
    qualification:{ type:String, required:false},
    passedyear:{ type:String, required:false},
    course:{ type:String, required:false}
})

const freelancerSchema = new Schema({
     name:{type:String},
    education:[educationSchema],
    skill:{type:Array,required:true}, 

},

    {   
        timestamps:true,

    });


    const freelancer = mongoose.model('freelancer',freelancerSchema);
    module.exports=freelancer;

контроллер ниже

const Freelancer = require('../../models/freelancer');

exports.updatefreelancer=function (req, res) {
         const id = req.params.id;
         const updateObject = req.body;
          Freelancer.updateOne({ _id:id }, { $set:updateObject })
           .exec()  
           .then(() => {
             res.status(200).json({
               message: 'freelancer is updated',
               updatenew: updateObject,
             });
           })
          .catch((err) => {
             res.status(500).json({   
               message: 'Server error'
             });
           });   
       }

В приведенном ниже коде мне нужно обновить объект, который внутри образования

данные mongodb

   "skill": [

    ],
    "_id": "5e68ee62e87207270c484c7d",
    "name": "fdfdf",
    "education": [
      {
        "_id": "5e6b8b3884edde05089385cd",
        "qualification": "UG",
        "passedyear": "2020",
        "course": "BE"
      },
      {
        "_id": "5e6b8b3884edde05089385ce",
        "qualification": "PG",
        "passedyear": "2021",
        "course": "me"
      }
...