有没有一种方法可以在不传递parentId的情况下从对象中删除子文档?

问题描述

有没有一种方法可以在不传递parentId的情况下从对象中删除子文档? 所以我有一个看起来像这样的文件

name: {
  type: [String],trim: true,required: [true,'Please add your name']
},experience: [
  {
    title: {
      type: String,'Please add an experience title']
    }
  }
],

本文档中的每个文档都可以在Experience数组中包含多个对象。

添加和更新对我来说很容易,因为我通常只需要传递父文档的ID。

现在,我想通过仅证明其ID ...而不是parentId来删除所述体验数组的任何对象。

希望这些代码可以帮助解释我在寻找什么:

  const resume = await Resume.findByIdAndUpdate(
    {
      // _id: req.params.id,// <=== is the parent id...
      experience: { _id: req.params.exp_id }
    },{
      $pull: { experience: { _id: req.params.exp_id } }
    },{
      new: true,runValidators: true
    }
  );

您可以在上面的代码中看到我传递了父文档的_id,如果继续这种方式,它会很好用,但是出于我的需要,我只需要传递子文档的_id。

实际上可行吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)