Friday, 28 April 2017

Remove all documents in sub collection with Mongoose

I have a collection which looks like:

    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
    }],
})

What 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});

        });

});



via user

No comments:

Post a Comment