0

I have mongoose schemas like this:

var question = new Schema({
   title : String,
   gambar : String
});

var polling = new Schema({
id_poll : String,
soal : [question]
});

i want to remove all subdocs soal by its index of array, using findOneAndRemove method like this,

polling.findOneAndRemove({_id:req.params.poll_id, soal[req.params.soal]._id: req.params.id_soal}, function(err){
if(err) throw err;
console.log("soal deleted");
});

but it throws error like this:

polling.findOneAndRemove({_id:req.params.poll_id, soal[req.params.soal]._id:
                                                      ^
SyntaxError: Unexpected token [
    at Module._compile (module.js:439:25)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:935:3

so how can i remove the subdocs without knowing its index, i have google it and find using operator $pull but i still don't understand how to apply it to my code. need advice and help. thank you so much

1 Answers1

0

As I understand from the details that you provided, you have two possible ways of achieving the deletion of the soal field:

  1. Empty the soal array by setting its value to []

    polling.update({_id: req.params.poll_id }, { $set: { soal: [] }}, function(err, response) {
        if (err) {
            console.log('There was an error with your request');
        }
    
        console.log('Mongo update from running the update query', response);
    }));
    
  2. Permanently delete the soal property from the document that you retrieve

    polling.update({_id: req.params.poll_id }, { $unset: { soal: "" }}, function(err, response) {
        if (err) {
            console.log('There was an error with your request');
        }
    
        console.log('Mongo update from running the update query', response);
    }));
    
vladzam
  • 5,462
  • 6
  • 30
  • 36
  • thank you, but how to do that in mongoose ? as far as i know its a mongodb query, that used in CLI. – Albert Leonardo Pisa Oct 01 '15 at 08:41
  • @AlbertLeonardo I only included the CLI query as mongoose follows its syntax closely. I have now updated it. Hope it helps! – vladzam Oct 01 '15 at 09:52
  • thank you!, why in no 1 use $set but in no 2 use $unset ? what is the different? – Albert Leonardo Pisa Oct 01 '15 at 11:19
  • I presented two different approaches. As mentioned in my response, in the first case you set the value of the soal array to an empty one, thus removing all elements. In the second scenario, you permanently remove a field using the $unset operator, regardless of the type of that field. – vladzam Oct 01 '15 at 11:26
  • soal array contain an array filled with answer objects, so if i want to delete soal[0] it will be { $set: { soal: [req.params.soal] }} right?? as req.params.soal contain the index of array that i want to remove. – Albert Leonardo Pisa Oct 01 '15 at 11:58
  • anyway i just use your first solution but it didn't work. here is the full code. app.get('/soal/delete/:poll_id/:id_soal/:soal', isLoggedIn, function(req, res){ polling.update({_id: req.params.poll_id }, { $set: { soal: [req.params.soal] }}, function(err, response) { if (err) { console.log('There was an error with your request'); } console.log('Mongo update from running the update query', response); }); }); – Albert Leonardo Pisa Oct 01 '15 at 11:59
  • You need to leave soal : [] with empty brackets, not provide req.params.soal. Leave the exact code that I provided - it will empty the soal array – vladzam Oct 01 '15 at 12:07