【问题标题】:MongoDB: update from ObjectId to string for many documentsMongoDB:从 ObjectId 更新为许多文档的字符串
【发布时间】:2020-05-21 21:35:29
【问题描述】:

我有一个类似的复杂结构:

{
    type: "Data",
    data: [
      "item1": {...},
      "item2": {...},
      ...
      "itemN": {
          "otherStructure": {
              "testData": [
                  {
                      "values": {...}
                  },

                  {
                      "values": {
                          "importantKey": ObjectId("23a2345gf651")
                      }
                  }
              ]
          }
      }
    ]
}

对于这种数据结构,我想将所有这些 importantKeys 的数据类型从 ObjectId 更新为字符串。

我正在尝试类似以下的查询:

db.collection.updateMany(
{type:"Data"},
{$toString: "data.$[element].otherStructure.testData.$[element].values.importantKey"},
{"data.element.otherStructure.testData.element.values.importantKey": {$type: "objectId"}})

但所有这些尝试都没有成功。

那么,有没有合适的解决方案来更新这些数据?

更新 对不起,我的结构更复杂:

data.content.$[element].otherStructure.testData.keys.$[element].values.$[element].meta.importantKey

所有这些$[element] 元素表示具有元素列表的对象。

【问题讨论】:

    标签: database mongodb mongodb-query aggregation-framework mongoid


    【解决方案1】:

    您可以使用此解决方法:

    db.collection.aggregate([
      {
        $addFields: {
          "data.content": {
            $map: {
              input: "$data.content",
              as: "data",
              in: {
                otherStructure: {
                  testData: {
                    keys: {
                      $map: {
                        input: "$$data.otherStructure.testData.keys",
                        as: "testData",
                        in: {
                          "values": {
                            $map: {
                              input: "$$testData.values",
                              as: "values",
                              in: {
                                "meta": {
                                  "importantObject": {
                                    "importantKey": {
                                      $toString: "$$values.meta.importantObject.importantKey"
                                    }
                                  }
                                }
                              }
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      },
    {$out:"collection"}
    ])
    

    MongoPlayground

    【讨论】:

    • 但是如果一些结构相似的文档没有这个属性,它会插入它而不只是更新现有属性的数据类型?
    • @pythonista 不,如果没有任何具有预期结构的嵌套数组,$map 将停止执行。您尝试强制错误的结构并检查它是否正确更新
    • 这里如何处理第二个element?我有一些更复杂的结构,并试图了解这里的所有细节。 (更新原帖结构)
    • @pythonista 在此处分享您的示例数据mongoplayground.net/p/wcRk9m2_7_N
    • 我更新了你的配置部分。mongoplayground.net/p/PbZWE8-O7vO
    猜你喜欢
    • 1970-01-01
    • 2018-06-25
    • 2013-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多