0

I have a collection:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
ObjectId = Schema.ObjectId;
var User = new Schema({
    id: ObjectId,
    name: String,
    positionsApplied:[{
            position_id:ObjectId,
            index_position: Number
    }],
})

I need to do is remove all of the documents that exist within the positionsApplied sub collection with mongoose. I'm not sure where I'm going wrong:

app.post('/deleteAll', function(req, res){
  User.find(req.user._id, // this is the object Id which matches the logged in user
        {$unwind : "$positionsApplied"}).remove().exec(function (err, result) {
          console.log(result);
          res.send({results:result});

        });

});

update: schema: enter image description here

return from remove(): enter image description here

user
  • 395
  • 2
  • 11
  • 28

1 Answers1

0

Schema

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
ObjectId = Schema.ObjectId;
var PositionApplied = new Schema({
    position_id:ObjectId,
    index_position: Number
}, { _id : false });

var User = new Schema({
    _id: ObjectId,
    name: String,
    random_index_number:Number,
    companyname: String,
    password: String,
    email: String,
    username: String,
    location: String,
    role:String,
    teamwork:Number,
    initiative:Number,
    technical_skills: Number,
    communication:Number,
    employees: String,
    profile_image_link: String,
    video_url: String,
    biodescription: String,
    twitter: String,
    facebook: String,
    instagram: String,
    linkedin: String,
    google: String,
    biodescription: String,
    positionsApplied:[PositionApplied],
    experience: [{
          title: String,
          location: String,
          company: String,
          start: String,
          end:String,
          description:String
  }],
    position: [{
          _id:String,
          title: String,
          location: String,
          start: String,
          term:Number,
          description:String,
          teamwork_value:Number,
          communication_value:Number,
          initiative_value:Number,
          pos_index_number:Number,
            preference:[{
                        candidate_id: String,
                        weightedScore:Number,
                        position_id:String,
                        candidate_index_number:Number
                    }],
                    date: {type: Date, default: Date.now},
                        applied:[{
                                candidate_id: ObjectId,
                                date: {type: Date, default: Date.now},
                                profile_image: String,
                                location: String,
                                name: String,
                                _id:ObjectId
                        }],
  }],
  education: [{
          school: String,
          location: String,
          degree: String,
          start: String,
          end:String,
          description:String,
                    _id:String
  }],
    survey: [{
                    teamwork:Number,
                    communication:Number,
                    initiative:Number
    }],

    images: [{
                    one: String,
                    two: String,
                    three: String,
                    four: String,
                    five:String,
                    six:String
    }],
});


module.exports = mongoose.model('User', User);
module.exports = {
  User : mongoose.model('User', User),
  PositionApplied : mongoose.model('PositionApplied', PositionApplied)
}

Call:

     app.post('/deleteall', function(req, res){
          User.findOne({_id: req.user._id}, 'positionsApplied', function(err, user){
        if (err) return handleError(err);
        user.positionsApplied = user.positionsApplied.map(e => e.position_id);
        PositionApplied.remove({position_id: {$in: user.positionsApplied}}, function(){

        });
      }
  );
      });

Updated error message: enter image description here

ifiok
  • 499
  • 4
  • 14
  • thanks for the reply! I'm getting the error: "Cannot read property 'map' of undefined" , ive updated my question, any idea where its going wrong? – user Apr 30 '17 at 12:48
  • You are using User. It should be user (lowercase) that is returned in the callback – ifiok Apr 30 '17 at 12:50
  • the remove seems to be running with no errors, however its picking up weird id's that dont exist in the database, Ive attached images to the question, is there something I'm missing? Thanks for the help! – user Apr 30 '17 at 13:00
  • can you do a console.log of user.positionsApplied ? – ifiok Apr 30 '17 at 13:13
  • yes the image above (under return from remove() line) is what is returned to me if i console.log(user.positionsApplied) .. I'm not sure where the _id's are coming from – user Apr 30 '17 at 13:16
  • I have edited my response. Check the schema definition of PositionApplied. – ifiok Apr 30 '17 at 16:46
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/143038/discussion-between-ifiok-and-user). – ifiok Apr 30 '17 at 16:53