Mongoose,更新另一个数组内的数组内的对象 [英] Mongoose, Update an object inside an array which is inside another array

查看:86
本文介绍了Mongoose,更新另一个数组内的数组内的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法更新和对象内部和数组,它也像这个例子中的另一个数组一样?

Is there a way to update and object inside and array which is also inside another array like in this example?

这就是集合的样子:

{
  "_id": "5fd3966d2769d212dc0104a4",
  "Races": [
    {
      "_id": "5fd3966d2769dc12dc0105f4",
      "gpName": "Sakhir Grand Prix",
      "RaceResult": [
        {
          "_id": "5fd3966d2769dc12dc0105e0",
          "position": "First",
          "driverName": "Hamilton"
        },
        {
          "_id": "5fd3966d2769dc12dc0105e1",
          "position": "Second",
          "driverName": "Vettel"
        }
      ]
    },
    {
      "_id": "5fd3966d2769dc12dc0105df",
      "gpName": "Abu Dhabi Grand Prix",
      "RaceResult": [
        {
          "_id": "5fd39452d2769dc12dc0105df",
          "position": "First",
          "driverName": "Bottas"
        },
        {
          "_id": "5fd3966d2769dc12dc0105e2",
          "position": "Second",
          "driverName": "Hamilton"
        }
      ]
    }
  ]
}

所以如果我想更改例如美国大奖赛"而不是Sakhir Grand Prix",我会选择:

So If I want to change for example, "United States Grand Prix" instead of "Sakhir Grand Prix", I would go with:

StandingsModel.updateOne({ "Races._id": "5fd3966d2769dc12dc0105f4"}, {
        "$set": {
            "Races.$.gpName": 'United States Grand Prix'}
})

但是我怎样才能更改driverName"呢?或位置"RaceResults 中的值?.

But how can I change the "driverName" or "position" value inside RaceResults?.

我试过设置:

(为了澄清,在这个例子中,我想更改阿布扎比大奖赛内的第二个驱动程序名称)

(For clarification, in this example I wanna change the second driver name inside Abu Dhabi GP)

StandingsModel.updateOne({ "Races.RaceResults._id": "5fd3966d2769dc12dc0105e2"}, {
        "$set": {
            "Races.$.RaceResults.$.driverName": 'Vettel'}
})

但这当然不能按预期工作,猫鼬发回错误代码 2.

But of course this don't work as expected and mongoose send back an error code 2.

有什么想法吗?

谢谢.

推荐答案

您可以使用 $[].还有一个特定的部分叫做 使用 $[] 更新嵌套数组.

You can use $[<identifier>]. Also there is an specific section called Update Nested Arrays in Conjunction with $[].

您可以使用这种方式过滤数组,首先查找您的 Race id,然后再查找您的 RaceResult id.

You can use this way to filter the array looking for your Race id first and after your RaceResult id.

您可以使用此查询:

db.collection.update(
{ "_id": "5fd3966d2769d212dc0104a4" },
{ "$set": { "Races.$[race].RaceResult.$[result].driverName": "Vettel" } },
{ "arrayFilters": [ 
   { "race._id": "5fd3966d2769dc12dc0105df" },
   { "result.position": "Second" } ] 
})

$set 阶段会将对象修改为与过滤器 race 匹配的 Race 数组.在 Race 数组中,将通过 result 过滤器进行过滤.

The $set stage will modify the object into Race array that match the filter race. And inside race array, will filter by result filter.

即:查询将使用过滤器 race._id 通过其 id 获得一场比赛,并且在比赛中只有 position 所在的对象Second 将被更新.

That is: The query will get one race by its id using filter race._id and inside the race only the object where position is Second will be updated.

因此查询将更新您想要的对象.您可以在此处

So the query will update the object you want. You can check an example here

这篇关于Mongoose,更新另一个数组内的数组内的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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