我正在开发一个接受基本邀请的端点,我需要通过参数(起诉Express)中提供的ID获得通知,然后更新他的result字段.接口/架构如下所示:

const NotiticationSchema = new Schema<Notitication>({
    title: { type: String, required: true },
    description: { type: String, default: null },
    date: { type: Date, default: Date.now() },
    type: { type: String, required: true },
    navigation: {
        type: String,
        enum: NotificationNav,
        required: true
    },
    navigationId: {
        type: String,
        enum: NotificationIds,
        required: true
    },
    teamId: { type: String, required: true },
    result: {
        type: {
            accepted: { type: Boolean, required: true },
            motivation: { type: String, required: true }
        },
        default: null
    }
});

这是我目前使用的代码:

const notification = await NotiticationModel.findByIdAndUpdate(req.params["id"]!, { $set: { result: { accepted: true, motivation: null } } });

预期输出应为:

{
  ...
  "result": {
    "accepted": true,
    "motivation": null
  }
}

NotiticationModel.findByIdAndUpdate()做什么:

{
  ...
  "result": {
    "accepted": true,
    "motivation": null,
    "_id": {
      "$oid": "65b23b495a60bda2c6e67996"
    }
  }
}

我从来没有遇到过这种行为,有谁能帮忙吗?

另外,我已经问过人工智能了,它没有给出任何有用的建议.

推荐答案

这是意料之中的行为.当您将一个对象添加到MongoDB中时,Mongoose会自动为其分配一个ObjectId类型的_id属性.

要删除此功能,请将_id: false属性传递到相关对象的模式中,如下所示:

const NotiticationSchema = new Schema<Notitication>({
    //...
    teamId: { type: String, required: true },
    result: {
        type: {
            accepted: { type: Boolean, required: true },
            motivation: { type: String, required: true }
        },
        default: null,
        _id: false //< here
    }
});

Javascript相关问答推荐

获取表格的左滚动位置

仅圆角的甜甜圈图表

Redux工具包查询(RTKQ)端点无效并重新验证多次触发

在页面上滚动 timeshift 动垂直滚动条

在网页上添加谷歌亵渎词

加载背景图像时同步旋转不显示的问题

如何通过将实例方法的名称传递给具有正确类型的参数的继承方法来调用实例方法?

环境值在.js文件/Next.js中不起作用

在Reaction中的handleSubmit按钮内,useSelector值仍然为空

元素字符串长度html

在D3条形图中对具有相同X值的多条记录进行分组

Clip-Path在网页浏览器(Mozilla、Edge、Chrome)上不能正常工作,但在预览版Visual Code Studio HTML、CSS、JS上却能很好地工作

每隔3个项目交替显示,然后每1个项目交替显示

bootstrap S JS赢得了REACT中的函数/加载

Chrome上的印度时区名S有问题吗?

如何在Web项目中同步语音合成和文本 colored颜色 更改

脚本语法错误只是一个字符串,而不是一个对象?

Reaction-在同一页内滚动导航(下拉菜单)

我想为我的Reaction项目在画布上加载图像/视频,图像正在工作,但视频没有

如何根据获取的对象数量动态生成状态变量