【发布时间】:2021-11-06 03:25:30
【问题描述】:
当我尝试更新数据库中的文档时发生了错误。
这是架构:
const prodSchema = new Schema({
name: {
type: String,
required: true,
unique: true
},
description: {
type: [String]
},
})
然后我从别处得到一些产品:
const some_product = axios.get(blah blah)
并更新 mu 文档。 请注意,我为“描述”设置了一个条件:如果它有一个空值,它会被更新 - 否则它保持不变:
const newProduct = {
$set: {
name: some_product.name,
description: {
$cond: {
if: {
$eq: [null, '$description']
},
then: [some_product.description],
else: '$description'
}
}
}
}
去更新(我用猫鼬): Product.updateOne({name: some_product.name}, newProduct, {some params})
我看到了这个:
转换为 [string] 的值失败 "[{"$cond":{"if":{"$eq":[null,"$description"]},"then":["这里有一些 路径中产品的新描述"],"else":"$description"}}]" “描述”
我认为关键在于模式中的描述类型是字符串数组,而请求的“some_product”中的“描述”字段只是一个字符串。可能是这个问题。
但是我该如何解决呢? 非常感谢。
【问题讨论】:
-
@mohammadNaimi 对不起,我没明白你的意思(