我想将文档中值为 null 的数组字段替换为空数组 []。我尝试了这个查询,但它不起作用。请帮忙。
基本上我希望 testArray: null 为 testArray: []
db.myTestCollection.aggregate( { "_id": ObjectId('1120191011212112') },
[ { $set: { testArray: { $ifNull: [ { $concatArrays: [ "$testArray", [] ] } ] } } }
])
最佳答案
看起来您主要有语法错误。但您也可以将 $ifNull
逻辑移至过滤器谓词中,这样,如果现有 testArray
字段尚未 null,则不会考虑/应用任何更新
。所以:
db.collection.update({
_id: 1,
"testArray": null
},
[
{
$set: {
testArray: []
}
}
])
关于mongodb - 在 mongodb 中,如何将具有空值的数组字段替换为空数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73951045/