Mongoose -通过ID更新数组中的多个对象

huus2vyu  于 2023-01-26  发布在  Go
关注(0)|答案(1)|浏览(172)

首先,对不起,我的英语不好。其次,我想问,如何多重更新我的文件。
我有一个这样的行结构:
数据模型

[
    {
        "id": '00001',
        "exp": '192',
        "items": [
            { "qty": 23, "ID": ObjectId("kieryu35261twerts73940djr") },
            { "qty": 77, "ID": ObjectId("1240a1ffuw33dbcv6ut8888zz") },
            { "qty": 1, "ID": ObjectId("5600r0e0rr67cbd60a1234y5") },
            { "qty": 5, "ID": ObjectId("32bbd0e0rr92cdb60a4386c7") }
        ],
        "stats": [
            { "strenght": 1, "ID": ObjectId("3eruskdjfhrutiwo4059yuol3") },
            { "strenght": 2, "ID": ObjectId("3jdks0d9r2seifldvjmw0km2e") },
            { "strenght": 1, "ID": ObjectId("aslkdnasklnc2112uhnsjssad") },
            { "strenght": 5, "ID": ObjectId("1230msafmlklkmasfmcio3kms") }
        ]
    },
    {
        "id": '00002',
        "exp": '100',
        "items": [
            { "strenght": 10, "ID": ObjectId("21312kn3kaklmasklcklasmck") },
            { "strenght": 10, "ID": ObjectId("kldsafklsajdfklmsadlkaskl") }
        ],
        "stats": [
            { "strenght": 9, "ID": ObjectId("aslkclkamiior2oinrkl2adsa") },
            { "strenght": 0, "ID": ObjectId("asdoasjdosmdkl123123kmnsd") }
        ]
    }
]

我想通过查找文档的id来更新一个文档行,也想通过id来更新多个深度元素,如下所示:

{
    "id": '00001',
    "exp": '555',
    "items": [
        { "qty": 44, "ID": ObjectId("kieryu35261twerts73940djr") },
        { "qty": 55, "ID": ObjectId("1240a1ffuw33dbcv6ut8888zz") },
        { "qty": 66, "ID": ObjectId("5600r0e0rr67cbd60a1234y5") },
        { "qty": 77, "ID": ObjectId("32bbd0e0rr92cdb60a4386c7") }
    ],
    "stats": [
        { "strenght": 10, "ID": ObjectId("3eruskdjfhrutiwo4059yuol3") },
        { "strenght": 20, "ID": ObjectId("3jdks0d9r2seifldvjmw0km2e") },
        { "strenght": 12, "ID": ObjectId("aslkdnasklnc2112uhnsjssad") },
        { "strenght": 54, "ID": ObjectId("1230msafmlklkmasfmcio3kms") }
    ]
}

最后,仅供参考,我在此之前:

await DataModel.findOneAndUpdate(
    { 
        "id" : idvariable // == 00001
    },
    {
        "$set" : {
            "exp" : 555,
            "items": {
                //update qty == 44 where ID == kieryu35261twerts73940djr
                //update qty == 55 where ID == 1240a1ffuw33dbcv6ut8888zz
                //update qty == 66 where ID == 5600r0e0rr67cbd60a1234y5
                //update qty == 77 where ID == 32bbd0e0rr92cdb60a4386c7
            },
            "stats": {
                //update strenght == 10 where ID == 3eruskdjfhrutiwo4059yuol3
                //update strenght == 20 where ID == 3jdks0d9r2seifldvjmw0km2e
                //update strenght == 12 where ID == aslkdnasklnc2112uhnsjssad
                //update strenght == 54 where ID == 1230msafmlklkmasfmcio3kms
            }
        }
    }
)

拜托,我不知道如何用一个查询更新或其他技术来更新它。谢谢。

yks3o0rb

yks3o0rb1#

可能,但查询有点长。
使用聚合管道进行更新,
$map-迭代数组中的项并返回新数组。
$switch- Switch-case(语句)匹配当前迭代文档的ID,如果匹配则更新文档。如果所有case条件都不匹配,则保留现有文档。

db.collection.update({
  "id": idvariable// == 00001
  
},
[
  {
    "$set": {
      "exp": 555,
      "items": {
        $map: {
          input: "$items",
          in: {
            $switch: {
              branches: [
                {
                  case: {
                    $eq: [
                      "$$this.ID",
                      "kieryu35261twerts73940djr"
                    ]
                  },
                  then: {
                    $mergeObjects: [
                      "$$this",
                      {
                        qty: 44
                      }
                    ]
                  }
                },
                {
                  case: {
                    $eq: [
                      "$$this.ID",
                      "1240a1ffuw33dbcv6ut8888zz"
                    ]
                  },
                  then: {
                    $mergeObjects: [
                      "$$this",
                      {
                        qty: 55
                      }
                    ]
                  }
                },
                {
                  case: {
                    $eq: [
                      "$$this.ID",
                      "5600r0e0rr67cbd60a1234y5"
                    ]
                  },
                  then: {
                    $mergeObjects: [
                      "$$this",
                      {
                        qty: 66
                      }
                    ]
                  }
                },
                {
                  case: {
                    $eq: [
                      "$$this.ID",
                      "32bbd0e0rr92cdb60a4386c7"
                    ]
                  },
                  then: {
                    $mergeObjects: [
                      "$$this",
                      {
                        qty: 77
                      }
                    ]
                  }
                }
              ],
              default: "$$this"
            }
          }
        }
      },
      "stats": {
        $map: {
          input: "$stats",
          in: {
            $switch: {
              branches: [
                {
                  case: {
                    $eq: [
                      "$$this.ID",
                      "3eruskdjfhrutiwo4059yuol3"
                    ]
                  },
                  then: {
                    $mergeObjects: [
                      "$$this",
                      {
                        strenght: 10
                      }
                    ]
                  }
                },
                {
                  case: {
                    $eq: [
                      "$$this.ID",
                      "3jdks0d9r2seifldvjmw0km2e"
                    ]
                  },
                  then: {
                    $mergeObjects: [
                      "$$this",
                      {
                        strenght: 20
                      }
                    ]
                  }
                },
                {
                  case: {
                    $eq: [
                      "$$this.ID",
                      "aslkdnasklnc2112uhnsjssad"
                    ]
                  },
                  then: {
                    $mergeObjects: [
                      "$$this",
                      {
                        strenght: 12
                      }
                    ]
                  }
                },
                {
                  case: {
                    $eq: [
                      "$$this.ID",
                      "1230msafmlklkmasfmcio3kms"
                    ]
                  },
                  then: {
                    $mergeObjects: [
                      "$$this",
                      {
                        strenght: 54
                      }
                    ]
                  }
                }
              ],
              default: "$$this"
            }
          }
        }
      }
    }
  }
])

对于要更新的带有ID的itemsstats,请确保您需要解析为ObjectId [请注意,Mongo Playground无法识别您提供的ID,我手动将这些ID的数据集更改为string,但更新的概念是相同的]。
Sample Mongo Playground

相关问题