【问题标题】:Can't push/update data into existing mongo object无法将数据推送/更新到现有的 mongo 对象中
【发布时间】:2019-02-11 14:01:20
【问题描述】:

我有一个用户对象,其中包含几个数组,如下所示:

{
  _id: "5b90a261ff3712000495ca29", 
  name: "Udit", 
  email: "guru.udit@bmail.com", 
  password: "sjsndj",
  education:[],
  professionalexp:[],
  projects:[],
  skills:[]
}

我正在使用 express API 将数据推送到带有某个对象的教育数组中,以下是我尝试更新 mongo 数据的代码。当我尝试推送和更新数据时,我看不到任何响应。我应该如何进行并一次推送数组或多个数组?

app.put("/api/updatefield/", function(req, res) {
  User.update(
    { _id: req.body._id },
    {
      $push: {
        "education.$.University": "something that is there"
      }
    },
    function(err, result) {
      if (err) {
        res.send(err);
      }
      if (result) {
        res.json(result);
      }
    }
  );
});

也供参考,这是我通过 API 正文推送的数据,我在其余服务器上的 req.body 中获取这些数据

{
    "summary": "some summary",
    "education": [
        {
            "name": "Institue",
            "from": "19/07/2018",
            "to": "30/07/2018"
        }
    ],
    "professional": [
        {
            "name": "Company",
            "from": "19/07/2018",
            "to": "30/07/2018"
        }
    ],
    "cardCount": 1,
    "cardCount2": 1
}

【问题讨论】:

  • 如果你的数组不仅仅是文本/字符串数组,你最好看看子文档的处理方式。
  • 我在数组中有对象。我还有什么方法可以做到这一点?
  • 我更喜欢定义(在猫鼬中)我在数组中使用的类型,所以我可以在推送之前正确地转换它。
  • 我已经在Schema中定义了
  • 是的——我要你做的是在数组中单独定义类型,这样你就可以在推送之前对其进行强制转换。 (我遇到了嵌套数组(数组里面有一个数组)的情况,我必须这样做)

标签: node.js mongodb reactjs express mongoose


【解决方案1】:

你可以这样做:

User.update({_id: ObjectId("5b910b0acb5b7646e630cefe")}, {
    $push: {
        "education": {
            "summary": "some summary",
            "education": [{"name": "Institue", "from": "19/07/2018", "to": "30/07/2018"}],
            "professional": [{"name": "Company", "from": "19/07/2018", "to": "30/07/2018"}],
            "cardCount": 1, "cardCount2": 1
        }
    }
})

对于多个对象:

User.update({_id: ObjectId("5b910b0acb5b7646e630cefe")}, {
    $push: {
        "education": {
            $each: [{
                "summary": "some summary",
                "education": [{"name": "Institue", "from": "19/07/2018", "to": "30/07/2018"}],
                "professional": [{"name": "Company", "from": "19/07/2018", "to": "30/07/2018"}],
                "cardCount": 1, "cardCount2": 1
            }, {"a": 2}]
        }    
    }
})

用于推入超过 1 个对象:

User.update({_id: ObjectId("5b910b0acb5b7646e630cefe")}, {
        $push: {
            "education": {
                $each: [{
                    "summary": "some summary",
                    "education": [{"name": "Institue", "from": "19/07/2018", "to": "30/07/2018"}],
                    "professional": [{"name": "Company", "from": "19/07/2018", "to": "30/07/2018"}],
                    "cardCount": 1, "cardCount2": 1
                }, {"a": 2}]
            },
           "professionalexp" : {"a":2}
        }
    })

【讨论】:

  • 如果数组中有多个对象,它还能工作吗?
  • 你推送的数据不是一个数组,它是一个对象,但是如果你必须推送多个对象,你必须在对象数组上运行 $each
  • 谢谢。我试试这个
  • 嘿,找到我的更新。我也为多个对象完成了它。
  • 嗨,我实际上只想在教育和专业内推送多个对象
猜你喜欢
  • 1970-01-01
  • 2018-07-20
  • 1970-01-01
  • 1970-01-01
  • 2022-06-14
  • 2020-04-29
  • 2020-10-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多