简体   繁体   中英

Push data inside nested array , mongoose

Please I know its duplicate , but I dont know why my code is not working

exports.addTechnologyPost = function(req, res){


     console.log(req.params.name);
     var query = {
        name: 'testName',
        version: 'testVer',
        note: 'testNote',
        status: true
     };
     console.log( query );
     Project.findOneAndUpdate(
        { name: req.params.name },
        { $push : {"technologies.$.server" : query }  },  {  upsert: true, new: true },   
        function(err, project) {

        })


 }

My Schema

..........
technologies:{
    server:[{
        name: String,
        version: Number,
        note: String,
        status: Boolean
    }]
  }
..........

If its correct or any syntax error ?

Have you tried removing the $ in the push object key?

Project.findOneAndUpdate(
        { name: req.params.name },
        { $push : {"technologies.server" : query }  },  {  upsert: true, new: true },   
        function(err, project) {

        })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM