简体   繁体   English

如何使用 node.js 删除 MongoDB 中嵌套对象数组中的 object

[英]How to delete an object in nested array of objects in MongoDB with node js

I'm still a beginner in node express js and mongoDB. Right now, I'm trying to Delete an object in nested array of objects.我仍然是 node express js 和 mongoDB 的初学者。现在,我正在尝试删除嵌套对象数组中的 object。

Array of Objects:对象数组:

[{
  _id: new ObjectId("63d89f8823981819cf61816e"),
  iqc: [
    {
      partname: 'jio',
      vendorname: 'jio',
      partcode: '1234',
      grndate: '2023-01-10',
      project: 'jio',
      lotqty: '200',
      failurerate: '15%',
      issuedetails: 'damaged',
      status: 'pending',
      _id: new ObjectId("63d89f8823981819cf61816f")
    },
    {
      partname: 'sky',
      vendorname: 'sky',
      partcode: '5678',
      grndate: '2023-01-04',
      project: 'sky',
      lotqty: '300',
      failurerate: '20%',
      issuedetails: 'damaged',
      status: 'pending',
      _id: new ObjectId("63d89f8823981819cf618170")
    }
  ],
  __v: 0
}]

I want to delete the object in iqc which has the _id: new ObjectId("63d89f8823981819cf618170") .我想删除 iqc 中的 object,它具有_id: new ObjectId("63d89f8823981819cf618170")

So i tried this code for deleting in node.js. It didnt work.It throws an error data.iqc.findByIdandDelete is not a function所以我尝试在 node.js 中删除此代码。它没有工作。它抛出错误 data.iqc.findByIdandDelete 不是 function

app.delete('/delete/:id/:secondid', async (req, res) => {
    const data = await IQC.findById(req.params.id);

if(data )
    {
        await data.iqc.findByIdandDelete(req.params.secondid)
        return res.json("Deleted  Successfully")
    }

});

Here IQC is the db collection and secondid is the id of the nested object id which I wanted to delete _id: new ObjectId("63d89f8823981819cf618170") .这里 IQC 是数据库集合,secondid 是我想删除的嵌套 object id 的 id : new ObjectId("63d89f8823981819cf618170")

Thanks in Advance.提前致谢。

You don't need to delete but update using $pull because you are updating the array (removing the object).您不需要删除但使用$pull进行更新,因为您正在更新数组(删除对象)。

So you can try something like this所以你可以尝试这样的事情

yourModel.update({
  "iqc._id": req.params.id
},
{
  "$pull": {
    "iqc": {
      "_id": req.params.id
    }
  }
})

Example here这里的例子

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 node.js 删除 mongodb 集合中的嵌套数组 - How to delete a nested array in mongodb collection with node.js 如何在Mongodb中使用Node.js在对象数组中添加object? - How to add object in Array of Objects using Node js in Mongodb? 如何使用Node.js中的Mongoose使用嵌套数组中object的object id查询和更新嵌套数组对象 - how to query and update a nested array objects using Mongoose in Node js using the object id of the object in the nested array MongoDB & Mongoose,保存对象的嵌套数组 - Node.js - MongoDB & Mongoose, Save Nested Array of Objects - Node.js 如何使用node.js在mongodb的嵌套数组内添加新对象? - How to add new objects inside nested array for mongodb using node.js? 如何使用 mongoose 节点 js 中的聚合使用多个嵌套对象过滤多个嵌套数组 object - How to filter multiple nested array object by using multi nested objects using aggregate in mongoose node js 如何在 mongodb 中使用 node.js 删除嵌入(嵌套)文档 - How to delete embedded (nested) document using node.js in mongodb 如何从 node.js 中删除 object 数组 - how to delete object array from node js Node Js 和 Moongoose:如何循环进入对象数组并删除特定的 object,然后更新整个文档 - Node Js and Moongoose: How to loop into array of objects and delete a particular object and then update the whole document Node/JS mongoose REST API 如何将新对象推送到一个集合中的嵌套对象数组中 - Node/JS mongoose REST API How to push a new object into a nested array of objects all in one collection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM