【问题标题】:Making a post/put request to an object inside an array向数组内的对象发出 post/put 请求
【发布时间】:2020-06-18 21:41:26
【问题描述】:

我在 mongo DB 中存储了许多类似以下的文档。

{
        "posts": [
            {
                "_id": "5ee38b041385d900004e78de",
                "postName": "Driver one",
                "hasSublevels": false,
                "isChildOfOther": false,
                regions: []
            },
            {
                "_id": "5ee38b0e1385d900004e78df",
                "postName": "Driver 2",
                "hasSublevels": true,
                "isChildOfOther": false,
                regions: []
            },
            {
                "_id": "5ee38b1b1385d900004e78e0",
                "postName": "Driver 3",
                "hasSublevels": true,
                "isChildOfOther": true,
                regions: []
            },
            {
                "_id": "5ee38b281385d900004e78e1",
                "postName": "Driver 4",
                "hasSublevels": true,
                "isChildOfOther": true,
                regions: []
            },
            {
                "_id": "5ee38b3a1385d900004e78e2",
                "postName": "Driver 5",
                "hasSublevels": true,
                "isChildOfOther": false,
                regions: []
            },
            {
                "_id": "5ee38b461385d900004e78e3",
                "postName": "Driver 6",
                "hasSublevels": true,
                "isChildOfOther": true,
                regions: []
            }
        ],
        "_id": "5ee38b499bd40260ad591d7e",
        "name": "Cabs NY",
        "date": "2020-06-12T14:03:53.343Z",
        "__v": 0
    },

每个帖子都有一个区域数组,如上所示。我想将值添加到帖子数组中的区域字段。 我如何提出首先通过 id 检索特定文档的请求?其次,允许我向特定帖子对象中的区域数组添加一个值吗?

【问题讨论】:

    标签: javascript reactjs mongodb rest express


    【解决方案1】:

    此代码用于第一部分,第二部分您可以使用 id.regions 然后像我一样将数据推送到其中。

    result ={
            "posts": [
                    {
                        "_id": "5ee38b041385d900004e78de",
                        "postName": "Driver one",
                        "hasSublevels": false,
                        "isChildOfOther": false,
                        regions: []
                    },
                    {
                        "_id": "5ee38b0e1385d900004e78df",
                        "postName": "Driver 2",
                        "hasSublevels": true,
                        "isChildOfOther": false,
                        regions: []
                    },
                    {
                        "_id": "5ee38b1b1385d900004e78e0",
                        "postName": "Driver 3",
                        "hasSublevels": true,
                        "isChildOfOther": true,
                        regions: []
                    },
                    {
                        "_id": "5ee38b281385d900004e78e1",
                        "postName": "Driver 4",
                        "hasSublevels": true,
                        "isChildOfOther": true,
                        regions: []
                    },
                    {
                        "_id": "5ee38b3a1385d900004e78e2",
                        "postName": "Driver 5",
                        "hasSublevels": true,
                        "isChildOfOther": false,
                        regions: []
                    },
                    {
                        "_id": "5ee38b461385d900004e78e3",
                        "postName": "Driver 6",
                        "hasSublevels": true,
                        "isChildOfOther": true,
                        regions: []
                    }
                ]
            }
    results = result.posts;
     
     groups = {};   
      for (var i in results) {           
      var groupName = results[i]._id;          
      if (!groups[results[i]._id]) {             
      groups[groupName] = [];  
      }  
      groups[groupName].push({"regions" :results[i].regions});             
      }           
      console.log(groups);
       

    【讨论】:

      【解决方案2】:
      const id = "5ee38b3a1385d900004e78e2";
      

      找到与上述 id 相关联的帖子。

      const post = posts.find(post => post._id === id);
      

      要添加到区域数组的负载

      const payload = {city: Nairobi, Country: Kenya};
      

      将有效负载添加到区域数组

      post.regions.push(payload);
      

      如果你使用猫鼬::

      const post = Posts.findById(id);
      if(!post) res.status(404).json({
            msg: "Specified post not found."
          });
      
      

      让我们使用上面的有效载荷。

      将有效负载添加到区域数组

      post.regions.push(payload)
      

      保存

      post.save()
      

      返回新帖子。

      【讨论】:

      【解决方案3】:

      根据 MongoDB 文档 https://docs.mongodb.com/manual/reference/operator/update/push/,您可以使用 $push 运算符向数组添加值,就像 Alpha 的示例一样。

      看起来像

      db.posts.update(
         { _id: "5ee38b281385d900004e78e1" },
         { $push: { regions: "West" } }
      )
      

      或追加多个

      db.posts.update(
         { _id: "5ee38b281385d900004e78e1" },
         { $push: { regions: { $each: ["West", "East", "South"] } } }
      )
      

      【讨论】:

        猜你喜欢
        • 2021-04-08
        • 2019-12-19
        • 2023-03-31
        • 1970-01-01
        • 1970-01-01
        • 2017-09-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多