【问题标题】:MongoDB removing subdocument issueMongoDB 删除子文档问题
【发布时间】:2020-10-12 13:17:36
【问题描述】:

我正在尝试通过它的 _id 删除特定的子文档。在 eventArrayModel 中有很多文档。 我正在按字段 resource 寻找子文档来源的匹配项。这是我想出的,但它不起作用。有什么想法吗?

const removedPost = await eventArrayModel.update({resource: req.params.teamMember}, {$pull: { array : {_id: ObjectId("5ef0a884c09b8e9ff01c8007")}}});

Structure of my database

【问题讨论】:

    标签: node.js database mongodb rest mongoose


    【解决方案1】:

    在您提供的screenshot 中,resource 字段位于array 字段内。尝试将更新查询更改为:

    await eventArrayModel.update(
      {
        'array.resource': req.params.teamMember
      },
      { $pull: { array: { _id: ObjectId('5ef0a884c09b8e9ff01c8007') } } }
    )
    
    

    【讨论】:

      猜你喜欢
      • 2023-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-15
      • 1970-01-01
      • 1970-01-01
      • 2017-08-22
      • 1970-01-01
      相关资源
      最近更新 更多