Note that there are some explanatory texts on larger screens.

plurals
  1. POMongoose won't remove embedded documents
    primarykey
    data
    text
    <p>I'm scratching my head here, as usual it seems with node projects, and I'm not sure if I'm doing something wrong or if I've run into a bug. </p> <p>I've got a schema of Server that can have any number of embedded docs called services. I'm running into a problem though where, even though I've successfully removed the individual service from the server object, when I tell it to save it doesn't remove it from the database. The save function is working because it's saving any changes I've made and is also pushing in new embedded docs, it's just not removing one that are already there. </p> <p>Here is a relatively simplified example of my code:</p> <pre><code>app.put('/server/:id', function(req, res, next){ app.Server.findOne({_id: req.params.id}, function(err, server) { server.updated = new Date(); ... for (var num = _.size(req.body.server.services) - 1; num &gt;= 0; num--){ // Is this a new service or an existing one if (server.services[num]) { // Is it marked for deletion? If so, delete it if (req.body.server.services[num].delete == "true") { server.services[num].remove() } else { // else, update it server.services[num].type = req.body.server.services[num].type ... } } else { // It's new, add it delete req.body.server.services[num]["delete"] server.services.push(req.body.server.services[num]); } } server.save(function(err){ if (!err) { req.flash('success', 'Server updated') } else { req.flash('error', 'Err, Something broke when we tried to save your server. Sorry!') console.log(err) } res.redirect('/') }); }) }); </code></pre> <p>So the remove() is actually removing the service. If I do a server.toObject() before the save, it's not there. Any ideas why it wouldn't be removing it from the database when it saves?</p> <p>Edit: I suppose the version numbers would be helpful. node@0.4.2, mongoose@1.1.5 express@2.0.0rc</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload