Home > Software design >  How to send array to mongodb using Node js api
How to send array to mongodb using Node js api

Time:08-17

I am new to Node.js and mongodb trying to send array of objects to a mongodb database. The data I am trying to send is for the following: databases,operatingSystems,integrations and frameworks which are receiving receiving requests from the fronted like this operatingSystems: [ 'RHEL Linux', 'Fedora Linux', 'CentOs Linux', 'Debian Linux' ], databases: [ 'Oracle RDBMS', 'Oracle DB BM DB2', 'Microsoft SQL Server' ], integrations: [ 'Data', 'COBRA', 'Power' ], frameworks: [ 'Angular', 'Ember.js', 'Node.js' ] how do I create a req.body that accepts the arrays for each of these. Additionally in mongodb the schema I have described as

     //Mongodb schema
     const ExperienceSchema = Schema({
      operatingSystems     : {type : Array, "default" : []} ,
      databases            : {type : Array, "default" : []},
      integrations         : {type : Array,"default" : []},
      frameworks           : {type : Array,"default" : []},
     otherTechnicalSkills : { type : String, required : false },


       description : { type : String, required : false, maxLength : 50 
       },
       })


   //Code for the controller
  export const addExperience = async(req, res) => {
try {

    const operatingSystemArray = 
   req.operatingSystems.array.split(',');
  
    
    const id = req.params.cvId;
    const filter = { _id : id};
    const update = { 
        $push : {
              techExperiences : {
               
                
                $each :{
                operatingSystems: (req.body.operatingSystems) ,
            
                 databases:(req.body.databases),
                 integrations:(req.body.integrations),
                framework:(req.body.framework),
                
        otherTechnicalSkills:req.body.otherTechnicalSkills,
                 
                     // Explain about their competence in the 
         selected group
                description:req.body.description,
              
                }
            }
        }
    }

    const opts = { new: true };

    let cv = await CV.findOneAndUpdate(filter, update, opts);

    res.status(200).send(cv);
    
} catch (error) {
    res.status(500).send(error)
}
    }`

CodePudding user response:

I am assuming that the techExperiences element of your CV model is the ExperienceSchema you shared.

I am also assuming that your req.body is where your operatingSystems, databases, integrations, and frameworks are coming from.

You could try this:

export const addExperience = async (req, res) => {
    try {

        const {operatingSystems, databases, integrations, frameworks} = req.body;

        const id = req.params.cvId;
        const filter = {_id: id};
        const update = {
            techExperiences: {operatingSystems, databases, integrations, frameworks}
        }

        const opts = {new: true};

        let cv = await CV.findOneAndUpdate(filter, update, opts);

        res.status(200).send(cv);

    } catch (error) {
        res.status(500).send(error)
    }
}

Whether it works or not will depend on your model schema for CV and javascript version. But maybe this will help point you in the right direction.

  • Related