0

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?

Return after debug: enter image description here

Database: enter image description here

user
  • 395
  • 2
  • 11
  • 28
  • Why are you using id as string, You are checking id and position._id in object id and database schema is string then mongoose can not find that document so how can it update? You have to use same schema type for all. – Yogesh Patel Apr 21 '17 at 18:32
  • Sorry @YogeshPatel I'm not sure I follow. What I'm trying to do is identify the user through the id variable, then from there get the position object within that user, find the id of the position I need and insert into the applied subdocument. I'm not sure where I'm going wrong with my query unfortunately, I appreciate the help though! – user Apr 21 '17 at 18:36
  • Looking your schema i find that in update query no document find so your data isn't push to mongodb. Your schema type must be match with query you have provided. You can check using find query. – Yogesh Patel Apr 21 '17 at 18:44
  • What part isn't matching though? @YogeshPatel the 'id' at the beginning of the update query is supposed to match the id of the user (the beginning of the schema) – user Apr 21 '17 at 18:46
  • Do you meant to use `id: String, position: [{ _id:String...}]` string values for id as they defined in schema ? Something like `User.update({id:"58c2871414cd3d209abf5fc9","position._id":"58d6b7e11e793c9a506ffe8f"}, {$push:{"position.$.applied":{ candidate_id:"test" } } } )` – s7vr Apr 21 '17 at 20:03
  • @Veeram even when I take out the mongoose.Types.ObjectId(...) and just leave the numbers its still not updating anything .. I just cant seem to figure out why.. in the console its saying query ok but nothing modified :( – user Apr 22 '17 at 12:02
  • Add `mongoose.set('debug', true)` at the top of file and console will output what query mongoose is sending to mongodb and check the document that you are expecting has the values you are looking for. – s7vr Apr 22 '17 at 12:05
  • @Veeram never knew you could do that, thanks! I've updated my question with a screenshot, but everything seems fine? .. Thanks for all your help by the way your always helping me out :) – user Apr 22 '17 at 12:15
  • You are welcome. It looks okay. Can you also add the document from the collection that you're expecting this query to update ? Is `_id` or `id` in your collection ? – s7vr Apr 22 '17 at 12:17
  • @Veeram I cant believe it, that was the issue! feeling rather stupid right now! haha Thanks again for your support, your always a great help! – user Apr 22 '17 at 12:23

0 Answers0