猫鼬expressJS更新嵌套数组中的单个字段

奈杰尔

我正在尝试使用Mongoose和expressJS作为后端在Web应用程序中构建编辑功能,我想更新lessonLog中的单个字段,该字段是具有单个objectId的lessonLog数组,我使用以下代码尝试了此操作,但未按预期工作, 请指教。先感谢您!

{
  Object _id : 601e7877cc4bc5047860831b ObjectId,
  date : 2021-02-05T16:00:00.000+00:00 Date,
  tempoMin : 71,
  tempoMax : 206,
  title : Khulau,
  intonation : test,
  phrasing : test,
  articulation : technique : test,
  notes : test ,
}
const studentsSchema = new mongoose.Schema({
  name: { type: String, require: true },
  email: { type: String, require: true },
  contact: { type: String, require: true },
  activities: {
    exam: { type: String, require: true },
    recording: { type: String, require: true },
    theory: { type: String, require: true },
    competition: { type: String, require: true },
    orchestra: { type: String, require: true },
  },
  teacher: { type: mongoose.Types.ObjectId, require: true, ref: "teachers" },
  repertoire: [{ type: String, require: true }],
  **lessonLog: [
    {
      date: { type: Date, require: true },
      tempoMin: { type: String, require: true },
      tempoMax: { type: String, require: true },
      title: { type: String, require: true },
      scales: { type: String, require: true },
      intonation: { type: String, require: true },
      phrasing: { type: String, require: true },
      articulation: { type: String, require: true },
      technique: { type: String, require: true },
      notes: { type: String, require: true },
    },**
  ],
});

这是查询:

const editLessonLog = async (req, res, next) => {
  try {
    await Students.findOneAndUpdate(
      {
        lessonLog: { _id: req.body.lessonLogId },
      },
      {
        $set: {
          lessonLog: {
            date: req.body.lessonDate,
            tempoMin: req.body.tempo[0],
            tempoMax: req.body.tempo[1],
            title: req.body.title,
            intonation: req.body.intonation,
            phrasing: req.body.phrasing,
            articulation: req.body.articulation,
            technique: req.body.technique,
            notes: req.body.notes,
          },
        },
      }
    );
    // console.log(student);
  } catch (err) {
    const error = new HttpError(
      "Cannot delete lesson log, please try again.",
      500
    );
    return next(error);
  }
穆罕默德·亚瑟·艾哈迈迪

您应该$根据文档使用

位置$运算符可标识要更新的数组元素,而无需显式指定数组中元素的位置

所以尝试一下

const editLessonLog = async (req, res, next) => {
  try {
    await Students.findOneAndUpdate(
      {
        "lessonLog._id": req.body.lessonLogId
      },
      {
        $set: {
          "lessonLog.$": {
            date: req.body.lessonDate,
            tempoMin: req.body.tempo[0],
            tempoMax: req.body.tempo[1],
            title: req.body.title,
            intonation: req.body.intonation,
            phrasing: req.body.phrasing,
            articulation: req.body.articulation,
            technique: req.body.technique,
            notes: req.body.notes,
          },
        },
      }
    );
    // console.log(student);
  } catch (err) {
    const error = new HttpError(
      "Cannot delete lesson log, please try again.",
      500
    );
    return next(error);
  }

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

用猫鼬更新双嵌套数组中的字段

来自分类Dev

用猫鼬更新嵌套数组

来自分类Dev

猫鼬按字段排序嵌套数组

来自分类Dev

更新混合类型的猫鼬嵌套数组

来自分类Dev

更新嵌套数组猫鼬内的元素

来自分类Dev

猫鼬和NodeJS中的嵌套数组中的问题

来自分类Dev

如何在猫鼬中查询嵌套数组

来自分类Dev

猫鼬嵌套查询-查找和更新不同文档中的两个字段

来自分类Dev

猫鼬在嵌套数组上聚合

来自分类Dev

用猫鼬计数嵌套数组

来自分类Dev

Node.js-猫鼬-使用req.body中的所有值更新嵌套数组

来自分类Dev

Node.js-猫鼬-使用req.body中的所有值更新嵌套数组

来自分类Dev

使用单个查询更新可选字段。猫鼬和expressjs

来自分类Dev

如何在findOne中过滤猫鼬中的嵌套数组而无需嵌套对象

来自分类Dev

猫鼬findOneAndUpdate更新多个字段

来自分类Dev

猫鼬更新记录数组中的嵌套对象

来自分类Dev

猫鼬用数组元素的条件填充嵌套数组

来自分类Dev

猫鼬用数组元素的条件填充嵌套数组

来自分类Dev

猫鼬:更新嵌套文档数组

来自分类Dev

猫鼬通过ObjectID从嵌套数组中拉出一个元素

来自分类Dev

使用玉+猫鼬显示嵌套数组

来自分类Dev

用猫鼬对嵌套数组进行排序

来自分类Dev

猫鼬查询帮助:分页,排序,嵌套数组的限制

来自分类Dev

猫鼬填充不填充嵌套数组数据

来自分类Dev

猫鼬按ID递增嵌套数组

来自分类Dev

使用玉+猫鼬显示嵌套数组

来自分类Dev

猫鼬如何使用$ elemMatch嵌套数组

来自分类Dev

将嵌套对象字段合并到MongoDB Aggregation中的单个嵌套数组字段中

来自分类Dev

如何在嵌套数组中实现多字段更新

Related 相关文章

热门标签

归档