简体   繁体   English

如何记录在 ZCCADDEDB567ABAE643E15DCF0974E503Z 模式中创建记录的时间戳?

[英]How can I record a timestamp of when a record is created in Mongoose schema?

I have an application that I'm building that allows users to enter records on a webpage.我正在构建一个允许用户在网页上输入记录的应用程序。 I want to display the date/time of when each record was submitted, I've tried several different methods and finally have the date/time to display in a format that I want, but it seems as though the timestamp is recorded when the server starts up instead of when the record is added.我想显示提交每条记录的日期/时间,我尝试了几种不同的方法,最后以我想要的格式显示日期/时间,但似乎时间戳是在服务器时记录的启动而不是在添加记录时启动。 I'm using moment.js for formatting我正在使用 moment.js 进行格式化

Here is my schema这是我的架构

const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const moment = require("moment");
var d = new Date();
var formattedDate = moment(d).format("MM-DD-YYYY, h:mm:ss a");
let codeSchema = new Schema({
    code: {
      type: String
    },
    createdAt: {
      type: String,
      default: formattedDate
    },
  },
  {
    collection: "codes"
  }
);
module.exports = mongoose.model("Code", codeSchema);

This is how the records look, except the date & time is when I start the server instead of when the document was actually added这是记录的外观,除了日期和时间是我启动服务器的时间而不是实际添加文档的时间

123456789789 12-09-2021, 12:59:33 pm

Interesting problem.有趣的问题。 I am using mongoose in an app and have not seen this issue.我在应用程序中使用 mongoose 并没有看到这个问题。 See schema below:请参阅下面的架构:

const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const schema = new Schema({
  createdBy: Object,
  created: { type: Date, default: Date.now },
  matchName: { type: String, required: true },
  matchType: { type: String, required: true },
  matchActive: { type: Boolean, default: false },
  matchPrivate: { type: Boolean, default: true },
  matchTime: { type: String, required: true },
  matchDate: { type: String, requried: true },
  matchPassword: String,
  matchMembers: [],
  course: Object,
  subCourse: Object,
  par: { type: Number, required: true },
  numberOfHoles: { type: Number, required: true },
  frontBackNine: { type: String, required: true, default: null },
  completed: { type: Boolean, default: false },
  compeletedOn: { type: Date },
  winner: { type: String },
  startingHole: { type: Number, default: 1 },
  scorecardSettings: { type: String, required: true },
  tournamentGroups: { type: Array, default: [] },
});

module.exports = mongoose.model("Matches", schema);

However I am not using moment.js.但是我没有使用 moment.js。

Two thoughts here:这里有两个想法:

  1. try removing commenting out moment.js code and use:尝试删除注释掉 moment.js 代码并使用:
Date.now

in place of the moment formatted date.代替时刻格式化的日期。 This is just to see if moment is causing the issue or not and will not actual give you a solution这只是为了查看是否是导致问题的时刻,并且不会实际为您提供解决方案

  1. How are you creating the collection and document?你是如何创建集合和文档的? Are you creating a document when the server is started or when the user uploads the file?您是在服务器启动时创建文档还是在用户上传文件时创建文档?

Think about when that variable is evaluated and assigned;考虑何时评估和分配该变量; is when the schema is created, that happens just one time.是在创建模式时,只发生一次。 In any case, you should assign that value at the time when you save the record, assign the value as any other.在任何情况下,您都应该在保存记录时分配该值,并像其他任何值一样分配该值。

But , moongose already has an option to assign timestamps to records when are created and updated.但是,moongose 已经可以选择在创建和更新记录时为记录分配时间戳。 See it here: https://mongoosejs.com/docs/guide.html#timestamps在这里查看: https://mongoosejs.com/docs/guide.html#timestamps

Your code should look like:您的代码应如下所示:

const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const codeSchema = new Schema({
    code: {
      type: String
    },
    createdAt: {
      type: String,
      default: formattedDate
    },
  },
  {
    collection: "codes",
    timestamps: true
  }
);

module.exports = mongoose.model("Code", codeSchema);

About the format, I highlight recommend take care of that after at the final point when you use the data.关于格式,我强调建议在使用数据的最后一点之后注意这一点。

You are assigning a predefined value, that's why it is not working, you should create a function and assign the function as a default value to get the value when the document.您正在分配一个预定义的值,这就是它不起作用的原因,您应该创建一个 function 并将 function 分配为默认值以获取文档时的值。

const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const moment = require("moment");

var createdAt = function(){
    var d = new Date();
    var formattedDate = moment(d).format("MM-DD-YYYY, h:mm:ss a");
    return formattedDate;
};

let codeSchema = new Schema({
    code: {
      type: String
    },
    createdAt: {
      type: String,
      default: createdAt
    },
  },
  {
    collection: "codes"
  }
);
module.exports = mongoose.model("Code", codeSchema);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM