【问题标题】:how to remove child object in mongodb如何在mongodb中删除子对象
【发布时间】:2019-09-25 16:57:30
【问题描述】:

下面是我在 mongodb 中的 json 存储

[
  {
    "type": "mailbox",
    "id": 0,
    "name": "Suresh Adling",
    "mailBoxType": "private",
    "children": [
      {
        "id": 1,
        "name": "Inbox",
        "type": "folder",
        "mailBoxType": "private",
        "children": [],
        "userName": "suresh.adling",
        "canEdit": false
      },
      {
        "id": 2,
        "name": "Deleted Items",
        "type": "folder",
        "mailBoxType": "private",
        "children": [],
        "userName": "suresh.adling",
        "canEdit": false
      },
      {
        "id": "fc761eaa-e2c8-493f-91d3-04d38c730534",
        "name": "vishal patil_deactivated",
        "mailBoxType": "private",
        "type": "Folder",
        "children": [
          {
            "id": "214f08d7-32e1-4871-aa1d-2c923882a302",
            "name": "Inbox",
            "type": "Folder",
            "mailBoxType": "private",
            "children": [],
            "userName": "suresh.adling",
            "canEdit": false
          },
          {
            "id": "fb91e540-1b23-438a-a1b3-7a08d0a60fbf",
            "name": "Deleted Items",
            "type": "Folder",
            "mailBoxType": "private",
            "children": [],
            "userName": "suresh.adling",
            "canEdit": false
          }
        ],
        "userName": "suresh.adling",
        "canEdit": false
      },
      {
        "id": "1d172869-18b4-4c11-a8bf-e162ec0fd260",
        "name": "swapnil N_deactivated",
        "mailBoxType": "private",
        "type": "Folder",
        "children": [
          {
            "id": "b5b86995-43e6-4798-ad94-20f2598a2cf1",
            "name": "Inbox",
            "type": "Folder",
            "mailBoxType": "private",
            "children": []
          },
          {
            "id": "13f384ea-c2f6-48c3-a67f-dc712b7917a8",
            "name": "Deleted Items",
            "type": "Folder",
            "mailBoxType": "private",
            "children": []
          }
        ]
      }
    ],
    "userName": "suresh.adling",
    "canEdit": false
  }
]

并在下面对象的这个json中删除数据库

{
        "id": "fc761eaa-e2c8-493f-91d3-04d38c730534",
        "name": "vishal patil_deactivated",
        "mailBoxType": "private",
        "type": "Folder",
        "children": [
          {
            "id": "214f08d7-32e1-4871-aa1d-2c923882a302",
            "name": "Inbox",
            "type": "Folder",
            "mailBoxType": "private",
            "children": [],
            "userName": "suresh.adling",
            "canEdit": false
          },
          {
            "id": "fb91e540-1b23-438a-a1b3-7a08d0a60fbf",
            "name": "Deleted Items",
            "type": "Folder",
            "mailBoxType": "private",
            "children": [],
            "userName": "suresh.adling",
            "canEdit": false
          }
        ],
        "userName": "suresh.adling",
        "canEdit": false
      },

如何删除rest api node js以及如何在node中创建这些api?

我是这个领域的新手。我想要正常的成功消息

【问题讨论】:

    标签: javascript node.js mongodb rest


    【解决方案1】:

    我认为您搜索了 $pull 运算符,您必须提供匹配的查询,例如要删除的数组元素的 id,更多在 mongodb 文档中。 https://docs.mongodb.com/manual/reference/operator/update/pull/#up._S_pull

    我在 mongoDB 中有我的 stores 集合,我创建了 store:

        {
        "children": [
            {
                "id": "First child",
                "value": 5
            },
            {
                "id": "Second child",
                "value": 10
            },
            {
                "id": "Third child",
                "value": 15
            },
            {
                "id": "Fourth child",
                "value": 20
            }
        ],
        "_id": "5d8b28c13ff93437f875d263",
        "name": "Johnny",
        "color": "Blue",
        "__v": 0
    }
    

    在 nodeJS 中我创建了 store 模型:

    const mongoose = require('mongoose');
    
    const Schema = mongoose.Schema;
    
    const StoreSchema = new Schema({
        name: {
            type: String,
            required: true
        },
        color: {
            type: String,
            required: true
        },
        children: {
            type: Array,
            required: true
        }
    });
    
    const Store = mongoose.model('stores', StoreSchema);
    
    module.exports = Store

    以及删除它的孩子的方法:

        router.delete("/children", async (req,res) =>{
        const {storeId, childId} = req.query
        const query = Store.update({ _id: storeId },{ $pull: { children: { id: childId}}});
        try {
            const result = await query.exec()
            res.status(200).send(result)
        } catch (err){
            res.send(err)
        }
    })

    从 postman 调用 API 后: Screenshot of API call

    Store 对象已更新,现在看起来像这样:

    { 
       "_id":"5d8b282a9fb35d0714c37550",
       "children":[ 
          { 
             "id":"Second child",
             "value":10
          },
          { 
             "id":"Third child",
             "value":15
          },
          { 
             "id":"Fourth child",
             "value":20
          }
       ],
       "name":"Johnny",
       "color":"Blue",
       "__v":0
    }
    

    【讨论】:

      猜你喜欢
      • 2019-11-25
      • 2010-09-23
      • 1970-01-01
      • 1970-01-01
      • 2021-02-19
      • 1970-01-01
      • 2012-08-07
      • 2020-12-13
      相关资源
      最近更新 更多