通过collection.update将所有数组值更新为Upper

问题描述 投票:0回答:1

Mongo shell> db.version:4.2.6尝试使用以下方法更新集合中的所有文档:将array [],[“ value1”,“ value2”]类别划分为[“ VALUE1”,“ VALUE2”]通过使用占位符$ [e]

可以在下面的重写方法或解释中使用一些帮助。谢谢。

db.Article.update( {},{ $set :{ "categories.$[e]" : { $toUpper: "e"  }}},{ multi: true, arrayFilters : [ {"e" : { $regex:'.+'  } } ]} )

WriteResult({
        "nMatched" : 0,
        "nUpserted" : 0,
        "nModified" : 0,
        "writeError" : {
                "code" : 52,
                "errmsg" : "The dollar ($) prefixed field '$toUpper' in 'categories.0.$toUpper' is not valid for storage."
        }
})
mongodb
1个回答
0
投票

您可以尝试以下查询:

db.Article.update(
  { $expr: { $eq: [{ $type: "$categories" }, "array"] } },
  [
    {
      $set: {
        categories: {
          $map: {
            input: "$categories",
            in: { $toUpper: "$$this" }
          }
        }
      }
    }
  ],
  { multi: true }
);
© www.soinside.com 2019 - 2024. All rights reserved.