문제

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.

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.

Here is a relatively simplified example of my 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 >= 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('/')
    });
  })
});

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?

Edit: I suppose the version numbers would be helpful. node@0.4.2, mongoose@1.1.5 express@2.0.0rc

도움이 되었습니까?

해결책

I found a way to temporary fix this problem.

What I did is load the embedded documents into an array, splice the one to be deleted and replace the array. Something like this:

var oldusers = dl.users;
oldusers.splice(dl.users.indexOf(req.currentUser.id), 1);
dl.users = oldusers;
dl.save(function(err) {...

I know that depending on the size of the document it will

다른 팁

I could be wrong, since I've not tested your example, but this sounds like Mongoose isn't detecting that the embedded document is modified.

From the schema types documentation page:

Since it is a schema-less type, you can change the value to anything else you like, but Mongoose loses the ability to auto detect/save those changes. To "tell" Mongoose that the value of a Mixed type has changed, call the .markModified(path) method of the document passing the path to the Mixed type you just changed.

person.anything = { x: [3, 4, { y: "changed" }] };
person.markModified('anything');
person.save(); // anything will now get saved

So you answer might be as simple as using the markModified() function.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top