Friday 21 April 2017

Insert into subdocument with Mongoose

I currently have a schema:

    var User = new Schema({
    id: String,
    position: [{
          _id:String,
          title: String,
          location: String,
          start: String,
          term:Number,
          description:String,
          date: {type: Date, default: Date.now},
            applied:[{
                candidate_id: String,
                _id:String
                        }],
  }]

I'm trying to insert data into the 'applied' subdocument but cant seem to get it working.

my function:

     app.post('/apply',function (req,res){


    //hard coded for testing purposes       
User.update({id:mongoose.Types.ObjectId("58c2871414cd3d209abf5fc9"),"position._id":mongoose.Types.ObjectId("58d6b7e11e793c9a506ffe8f")},
              {$push:{"position.$.applied":{
                candidate_id:"test"

              }
            }

            }).exec(function (err, result) {
          console.log(result);
          res.send({result:result});
        });
        });

For some reason this wont insert anything for me, the id's are definitely correct also. Anyone know where I'm going wrong?



via user

No comments:

Post a Comment