我正在尝试在nodejs中的MongoDB中更新几个对象。这是我遇到的问题:
我有一个看起来像这样的对象数组:
let date:Date
我正在尝试使用[{
"_id": {
"$oid": "5ed611265828aa77c978afb4"
},
"advert_id": "5ec2e4a8bda562e21b8c5052",
"isCategory": false,
"name": "2+1 Ev Taşıma",
"value": "2200 TL"
},
{
"_id": "40", // this object is recently added so it doesn't
// have an oid it needs to be inserted instead of updating.
"advert_id": "5ec2e4a8bda562e21b8c5052",
"isCategory": false,
"name": "4+1 Ev Taşıma",
"value": "7000 TL"
},
]
和collection.updateMany
更新这些对象中的每个对象
这是我写的代码:
upsert: true
这是错误:
mongodb.collection('prices').updateMany({}, {$set: post.prices}, {upsert: true}, (error, result) => {
if(error) throw error;
res.json(response);
})
问题似乎是我试图将价格数组直接传递到$ set管道中。什么是字段类型?如何将数组转换为该字段类型。
感谢帮助,在搜索文档方面有些困难,但是找不到任何相关章节。如果您也可以在答案中链接文档的相关部分,我将不胜感激。
数据库文档:
MongoError: Modifiers operate on fields but we found type array instead. For example: {$mod: {<field>: ...}} not
{$set: [ { _id: "5ed611265828aa77c978afb4", advert_id: "5ec2e4a8bda562e21b8c5052", isCategory: false, name: "2+1
Ev Taşıma", value: "2000 TL", isVisible: false } ]}
答案 0 :(得分:1)
我在这里{$set: post.prices}
看到问题
应为{$set: {'prices':post.prices}}
-您应向$set
提供文件
db.col.find({
'_id.$oid': '5ed611265828aa77c978afb4'
},
{
$set: {
"advert_id": "5ec2e4a8bda562e21b8c5052",
"isCategory": false
},
{
upsert: true
}
}