MongoDB默认将日期字段设置为当前日期

时间:2020-01-01 20:28:23

标签: javascript database mongodb date mongoose

我当时正在使用Express和Mongoose编写应用程序编码,并且我有一个包含几个字段的课程模型。其中包括名称,部分,dateCreated和lastUpdated。在我的模式中,dateCreated和lastUpdated如下所示:

void CalculateFrameRate()
{
    static float framesPerSecond = 0.0f;
    static int fps;
    static float lastTime = 0.0f;
    float currentTime = GetTickCount() * 0.001f;
    ++framesPerSecond;
    glPrint("Current Frames Per Second: %d\n\n", fps);
    if (currentTime - lastTime > 1.0f)
    {
        lastTime = currentTime;
        fps = (int)framesPerSecond;
        framesPerSecond = 0;
    }
}

现在,此代码的工作原理与预期相同。如果要在服务器上设置日期,则非常有用。但是,我得知我不应该依赖服务器设置的时钟,因为它们可能设置错误。我进行了一些研究,发现可以在更新或创建文档时使用$ currentDate。我想尽可能地高效,想要使用$ currentDate设置默认的dateCreated和lastUpdated。

我正在考虑使用courseSchema.post('save',()=> {})中间件,但是我不确定如何将dateCreated和lastUpdated设置为$ currentDate。帮助吗?

此外,请不要回答我应该只使用 dateCreated: { type: 'Date', default: moment }, lastUpdated: { type: 'Date', default: moment }, 还是坚持使用Date.now,因为使用$ currentDate是我想要的方式。

0 个答案:

没有答案