insert to MongoDB array with axios, restAPI and nodeJS


I am trying to add an item to a MongoDB array with RESTAPI through Axios. I thought it would look similar to the push method but I have no idea how to do that.

my Model is of a person:

const Schema = mongoose.Schema;
const PersonSchema = new Schema({
    name: String,
    password: String,
    friends: [],
    missions: []

const personModel = mongoose.model('Person', PersonSchema);

I want to add a mission to the mission array of a person.

and for example, in order to add a new Person, I use NodeJS and API:
(api.js)'/api/people', (req, res) => {
    const personToAdd = req.body;
    const newPersonPost = new personModel(personToAdd); => {
        if (e) {
        msg: 'Received'

and in the client side I use Axios:

                url: 'http://localhost:8080/api/people',
                method: 'POST',
                data: dataToUpdate
                .then(() => {
                    console.log('axios sent info to server');
                }).catch((e) => {
                    console.log('error' + e);

Thank you so much!


express'updating mission endpoint url', async (req, res) =>
try {
const query = { /* content */}; /* write a query to retrieve the concerned user by using a unique identifier */
let person = await personModel.findOne(query); 
} catch (err) { 


In the client side you just have to put the mission you want to add in data like you did above with the right endpoint url and you should add a unique identifier for the user you want to add mission to.

Answered By – CodeLearner

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published