【发布时间】: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