【问题标题】:save() ignores the "updateAt" field change [duplicate]save() 忽略“updateAt”字段更改[重复]
【发布时间】:2018-11-07 07:26:15
【问题描述】:

所以,我正在尝试(单元)测试使用 mongoosemocha 的一些功能。问题是当我尝试save( ) 架构时,updatedAt 字段被重写为当前时间。

即我有以下申请人

const applicant: DataModel = new DataSchema({
        "user_name": "user_notifying2rejected",
        "uuid": "10",
        "status": "notifying_user",
        "request_origin": "127.0.0.10"
});

然后,我做

applicant.updatedAt = new Date(new Date() - 2.592e+8);

当我console.log(applicant.updateAt) 时,我得到正确的必要日期,基本上是“3 天前”。

然后,像往常一样,我使用 mongoose 将其保存到数据库中:

await application.save();

但是,当我尝试验证文档(我刚刚保存的)时,却得到以下信息:

{
    createdAt: 2018-05-28T16:43:31.648Z,
    updatedAt: 2018-05-28T16:43:31.648Z,
    user_name: 'user_notifying2rejected',
    uuid: '10',
    request_origin: '127.0.0.10',
    _id: 5b0c31b06698440017d1593e,
    status: 'notifying_user'
}

现在 updatedAt 是今天的日期,而不是我想要的“3 天前”。如何阻止 mongoosemongoose.save 更改/覆盖我的 updatedAt 字段?

这是架构。如果您还需要什么,请告诉我。

架构

const DataSchema: Schema = new Schema({
    user_name: {
        type: String,
        required: true
    },
    uuid: {
        type: String,
        required: true
    },
    status: {
        type: String,
        required: true,
        default: "unprocessed"
    },
    request_origin: {
        type: String,
        required: true
    }
},
{
    timestamps: { createdAt: "createdAt", updatedAt: "updatedAt" }
});

【问题讨论】:

  • 您为架构开启了时间戳。如果您不想要它们,请关闭它们。阅读参考链接,了解它们的实际工作原理。

标签: node.js mongodb typescript mongoose mocha.js


【解决方案1】:

如果您更改 applicant 文档中的 updateAt,为什么还要使用 await application.save();

【讨论】:

    猜你喜欢
    • 2014-08-25
    • 1970-01-01
    • 2013-10-29
    • 2015-03-20
    • 1970-01-01
    • 1970-01-01
    • 2016-11-06
    • 1970-01-01
    • 2018-06-24
    相关资源
    最近更新 更多