Mongoose 不会删除嵌入文档

发布于 2024-10-22 00:07:40 字数 1782 浏览 1 评论 0原文

我在这里摸不着头脑,就像节点项目一样,我不确定我是否做错了什么或者是否遇到了错误。

我有一个服务器架构,它可以包含任意数量的嵌入式文档(称为服务)。我遇到了一个问题,尽管我已经成功地从服务器对象中删除了单个服务,但当我告诉它保存时,它并没有从数据库中删除它。保存功能正在工作,因为它保存了我所做的任何更改,并且还推送了新的嵌入式文档,它只是没有删除已经存在的文档。

这是我的代码的一个相对简单的示例:

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('/')
    });
  })
});

因此,remove() 实际上是删除服务。如果我在保存之前执行 server.toObject() ,它就不在那里。有什么想法为什么它在保存时不会将其从数据库中删除吗?

编辑:我想版本号会有帮助。 [电子邮件受保护][电子邮件受保护] [电子邮件受保护]

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. [email protected], [email protected] [email protected]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

感性不性感 2024-10-29 00:07:40

我可能是错的,因为我没有测试你的例子,但这听起来像 Mongoose 没有检测到嵌入文档被修改。

架构类型文档页面

由于它是无架构类型,因此您可以更改值您喜欢的任何其他内容,但是Mongoose 失去了自动检测/保存这些更改的能力。要“告诉”Mongoose Mixed 类型的值已更改,请调用文档的 .markModified(path) 方法,将路径传递给刚刚更改的 Mixed 类型。

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

所以你的答案可能就像使用 markModified() 函数一样简单。

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.

农村范ル 2024-10-29 00:07:40

我找到了暂时解决这个问题的方法。

我所做的是将嵌入的文档加载到一个数组中,拼接要删除的文档并替换该数组。像这样的事情:

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

我知道根据文档的大小,它会

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文