Node.js - Mongoose - 使用req.body中的所有值更新嵌套数组 [英] Node.js - Mongoose - Update nested array with all values in req.body

查看:518
本文介绍了Node.js - Mongoose - 使用req.body中的所有值更新嵌套数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个看起来像这样的对象。

I have an object that looks like this.

{
  _id: '577fe7a842c9b447',
  name: 'Jacob\'s Bronze Badges',
  competitors: [
    {
      _id: '577fe7a842c9bd6d',
      name: 'Peter\'s Silver Badges',
      sites: [
        {
          _id: '577fe7a842c9bd6d',
          name: 'Facebook',
          url: 'fb.com/peter'
        },
        {
          _id: '577fe7a842c9bd6d'
          name: 'Google',
          url: 'google.com/peter'
        }
      ]
    },
    {
      _id: '599fe7a842c9bd6d',
      name: 'Paul\'s Gold Badges',
      sites: [
        {
          '_id': '577fe7a842c9bd6d',
          name: 'Facebook',
          url: 'fb.com/paul'
        },
        {
          _id: '577fe7a842c9bd6d',
          name: 'Google',
          url: 'google.com/paul'
        }
      ]
    }
  ]
}

我的目标是引用竞争对手数组并使用所有值更新内部项目来自 req.body 。我的代码基于这个答案,以及另一个

My goal is to reference the competitors array and update items inside with all of the values from req.body. I based this code off of this answer, as well as this other one.

Location.update(
  { 'competitors._id': req.params.competitorId, },
  { $set: { 'competitors.$': req.body, }, },
  (err, result) => {
    if (err) {
      res.status(500)
      .json({ error: 'Unable to update competitor.', });
    } else {
      res.status(200)
      .json(result);
    }
  }
);

我将 HTTP PUT 发送给 localhost:3000 / compet / 577fe7a842c9bd6d 更新彼得的银徽章。请求正文是:

I send my HTTP PUT to localhost:3000/competitors/577fe7a842c9bd6d to update Peter's Silver Badges. The request body is:

{
  "name": "McDonald's"
}

问题是,当我使用 $ set 来设置竞争对手 _id:req.params.competitorId ,我不知道 req.body 中的内容。我想使用整个 req.body 来更新数组中的对象,但是当我这样做时,该对象被覆盖,因此不是获取新名称,而是Peter的Silver徽章变为:

The problem is that when I use $set to set the competitor with _id: req.params.competitorId, I don't know what is in req.body. I want to use the entire req.body to update the object in the array, but when I do, that object is overwritten, so instead of getting a new name, Peter's Silver Badges becomes:

{
  name: 'McDonald\'s',
  sites: []
}

当我知道对象的<$时,如何更新数组中的对象? c $ c> _id 包含 req.body 中的所有字段而不删除我想要保留的字段?

How can I update an object within an array when I know the object's _id with all of the fields from req.body without removing fields that I want to keep?

我认为 sites 数组为空,因为该对象已重新初始化。在我的架构中,我有 sites:[sitesSchema] 来初始化它。所以我假设整个竞争对手[_id] 对象被新名称覆盖,然后是网站:[sitesSchema] 来自myschema。

I believe that the sites array is empty because the object was reinitialized. In my schema I have sites: [sitesSchema] to initialize it. So I am assuming that the whole competitors[_id] object is getting overwritten with the new name and then the sites: [sitesSchema] from myschema.

推荐答案

您需要使用 $ $ set 中的位置运算符。为了根据 req.body 中的内容动态分配这些属性,您需要构建 $ set 以编程方式。

You would need to use the $ positional operator in your $set. In order to assign those properties dynamically, based on what is in your req.body, you would need to build up your $set programmatically.

如果您想更新名称,请执行以下操作:

If you want to update the name you would do the following:

Location.update(
  { 'competitors._id': req.params.competitorId },
  { $set:  { 'competitors.$.name': req.body.name }},
  (err, result) => {
    if (err) {
      res.status(500)
      .json({ error: 'Unable to update competitor.', });
    } else {
      res.status(200)
      .json(result);
    }
 }
);

您可以以编程方式构建 $ set 使用 req.body 执行以下操作:

One way you might programatically build up the $set using req.body is by doing the following:

let updateObj = {$set: {}};
for(var param in req.body) {
  updateObj.$set['competitors.$.'+param] = req.body[param];
 }

参见这个答案了解更多详情。

See this answer for more details.

这篇关于Node.js - Mongoose - 使用req.body中的所有值更新嵌套数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆