是否有機會使用findOneAndUpdate或findByIdAndUpdate通過"ID" 和 "lang"欄位觸發記錄來查詢 MongoDB 記錄?這是資料庫物件示例:
{
"id": "61842e6fb19afbc0922f5505",
"locales": [
{
"lang": "ru",
"title": "Тестовый десерт",
"description": "тестовый"
},
{
"lang": "lv",
"title": "Testu deserts",
"description": "Testu descriptions"
},
{
"lang": "en",
"title": "Test dessert",
"description": "Test description"
}
]
}
這就是我嘗試觸發的方式,但它不起作用:
const filter = {
_id: id,
locales: {
lang: lang
}
}
const product = await Product.findOneAndUpdate(filter, {
lang: {
title,
description,
price
}
}, { new: true });
uj5u.com熱心網友回復:
您可以$set像這樣使用運算子:
const product = await Product.findOneAndUpdate({
_id: "61842e6fb19afbc0922f5505",
"locales.lang": "ru"
},
{
"$set": {
"locales.$": {
lang: "ru",
title: "title",
description: "desription",
price: 23
}
}
})
作業游樂場:https : //mongoplayground.net/p/oBNS6wtK50g
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/348826.html
標籤:javascript MongoDB 猫鼬 图形
