MongoDB 嵌套搜索查询参数

MongoDB nested search query parameters

我是否有机会通过 "ID" findOneAndUpdatefindByIdAndUpdate 触发记录来查询 MongoDB 记录=22=] "lang" 字段?这是数据库对象示例:

{
        "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 });

您可以像这样使用 $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