【问题标题】:Mongoose use only createdAt timestampMongoose 仅​​使用 createdAt 时间戳
【发布时间】:2016-12-18 17:45:36
【问题描述】:

我在猫鼬中有以下消息架构:

var messageSchema = mongoose.Schema({
  userID: { type: ObjectId, required: true, ref: 'User' },
  text:   { type: String, required: true }
},
{
  timestamps: true
});

无论如何都可以忽略 updatedAt 时间戳?消息不会更新,所以更新了会浪费空间

【问题讨论】:

    标签: node.js mongodb mongoose


    【解决方案1】:

    使用 Mongoose v5 可能更好的是执行以下操作;

    const schema = new Schema({
      // Your schema...
    }, {
      timestamps: { createdAt: true, updatedAt: false }
    })
    

    【讨论】:

      【解决方案2】:

      编辑我已经修改了答案,以反映使用@JohnnyHK 的默认值的更好选择

      您可以通过在架构中声明 createdAt(或任何您想称呼的名称)来自己处理:

      mongoose.Schema({
        created: { type: Date, default: Date.now }
        ...
      

      或者,我们也可以在预保存挂钩中更新新文档的值:

      messageSchema.pre('save', function (next) {
        if (!this.created) this.created = new Date;
        next();
      })
      

      沿着这些线还有标志isNew,您可以使用它来检查文档是否是新的。

      messageSchema.pre('save', function (next) {
        if (this.isNew) this.created = new Date;
        next();
      })
      

      【讨论】:

      • 如果你定义一个默认值,你可以跳过中间件:created: { type: Date, default: Date.now }
      • 当然可以。我的错。我已经从默认值中得到了奇怪的不需要的影响,我现在明确地把它全部写出来,但在这种情况下它要好得多。
      【解决方案3】:

      较旧的主题,但根据您的架构可能会有更好的选择... 如果您坚持使用 mongodb/mongoose auto-gen _id 的默认设置,则已经内置了一个时间戳。如果您只需要“创建”而不是“更新”,只需使用...

      document._id.getTimestamp();

      从这里的 MongoDB 文档... ObjectId.getTimestamp()

      这里...stackoverflow

      【讨论】:

        【解决方案4】:

        Mongoose 时间戳接口有这些可选字段。

        interface SchemaTimestampsConfig {
            createdAt?: boolean | string;
            updatedAt?: boolean | string;
            currentTime?: () => (Date | number);
          }
        

        我们可以为我们想要的字段传递布尔值(createdAt: trueupdatedAt: true 将添加两个字段)。 我们可以使用 currentTime 函数来覆盖日期格式。

        示例:

        import mongoose from 'mongoose';
        
        const { Schema } = mongoose;
        const annotationType = ['NOTES', 'COMMENTS'];
        const referenceType = ['TASKS', 'NOTES'];
        const AnnotationSchema = new Schema(
          {
            sellerOrgId: {
              type: String,
              required: true,
            },
            createdById: {
              type: String,
              required: true,
            },
            annotationType: {
              type: String,
              enum: annotationType,
            },
            reference: {
              id: { type: String, index: true },
              type: {
                type: String,
                enum: referenceType,
              },
            },
            data: Schema.Types.Mixed,
          },
          { timestamps: { createdAt: true },
        );
        const AnnotationModel = mongoose.models.annotation || mongoose.model('annotation', AnnotationSchema);
        export { AnnotationModel, AnnotationSchema };
        
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-06-24
          • 2019-10-14
          • 2019-09-25
          • 2018-10-08
          • 2019-01-10
          • 1970-01-01
          • 1970-01-01
          • 2019-06-16
          相关资源
          最近更新 更多