我目前正在嘗試更新具有陣列的檔案。該陣列由幾個專案組成。我的更新過濾器productConfiguration.id然后更新。但是,我沒有得到任何更新。我嘗試了多種不同的方法,但沒有運氣。什么是正確的方法?
要更新的資料庫條目:
{
"id": "123",
"name": "Test",
"email": "[email protected]",
"productConfiguration": [{
"id": "1j6h591kz6wkznti5ta",
"name": "tester1",
"createdAt": {
"$date": "2022-02-15T07:42:38.492Z"
},
"updatedAt": {
"$date": "2022-02-15T07:42:38.492Z"
}
}, {
"id": "1j6h591kzepkzntqins",
"name": "tester1",
"createdAt": {
"$date": "2022-02-15T07:42:38.492Z"
},
"updatedAt": {
"$date": "2022-02-15T07:42:38.492Z"
}
}],
"__v": 0
}
貓鼬更新
app.put('/product', (req, res) => {
MySchema.findOneAndUpdate({
"productConfiguration.$.id" : req.body.id
}, {
$set: {
"productConfiguration.$.updatedAt": req.body.updatedAt
}
}, {
runValidators: true
}, function (err, user) {
if (!err) {
console.log('success', 'Config update successfully!');
res.redirect('/');
} else {
console.log('Error during record update : ' err);
}
});
});
uj5u.com熱心網友回復:
您的過濾器不正確,因為您在那里使用位置運算子。您可以簡單地使用點符號來查找正確的productConfiguration元素:
MySchema.findOneAndUpdate({
"productConfiguration.id" : req.body.id
}, {
$set: {
"productConfiguration.$.updatedAt": req.body.updatedAt
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/423991.html
