是否有一种方法可以将created_at和updated_at字段添加到猫鼬模式中,而不必在每次调用new MyModel()时传递它们?

created_at字段将是一个日期,仅在创建文档时添加。 每当对文档调用save()时,updated_at字段将被更新为新的日期。

我已经在我的模式中尝试了这一点,但除非我显式地添加它,否则字段不会显示:

var ItemSchema = new Schema({
    name    : { type: String, required: true, trim: true },
    created_at    : { type: Date, required: true, default: Date.now }
});

当前回答

var ItemSchema = new Schema({
    name : { type: String, required: true, trim: true }
});

ItemSchema.set('timestamps', true); // this will add createdAt and updatedAt timestamps

文档:https://mongoosejs.com/docs/guide.html时间戳

其他回答

使用函数返回计算出的默认值:

var ItemSchema = new Schema({
    name: {
      type: String,
      required: true,
      trim: true
    },
    created_at: {
      type: Date,
      default: function(){
        return Date.now();
      }
    },
    updated_at: {
      type: Date,
      default: function(){
        return Date.now();
      }
    }
});

ItemSchema.pre('save', function(done) {
  this.updated_at = Date.now();
  done();
});

对于带有Mongoose的NestJs,使用这个

@Schema({timestamps: true})

从mongo 3.6开始,你可以使用'change stream': https://emptysqua.re/blog/driver-features-for-mongodb-3-6/#change-streams

要使用它,你需要通过'watch'查询创建一个变更流对象,对于每个变更,你可以做任何你想做的事情…

python的解决方案:

def update_at_by(change):
    update_fields = change["updateDescription"]["updatedFields"].keys()
    print("update_fields: {}".format(update_fields))

    collection = change["ns"]["coll"]
    db = change["ns"]["db"]
    key = change["documentKey"]

    if len(update_fields) == 1 and "update_at" in update_fields:
        pass  # to avoid recursion updates...
    else:
        client[db][collection].update(key, {"$set": {"update_at": datetime.now()}})


client = MongoClient("172.17.0.2")
db = client["Data"]

change_stream = db.watch()

for change in change_stream:
    print(change)
    update_ts_by(change)

注意,要使用change_stream对象,你的mongodb实例应该作为'replica set'运行。 它也可以作为一个1节点的副本集(几乎没有变化,然后独立使用):

运行mongo作为一个副本集: https://docs.mongodb.com/manual/tutorial/convert-standalone-to-replica-set/

复制集配置vs独立: Mongo DB -独立和单节点副本集的区别

如果你使用nestjs和@Schema装饰器,你可以实现这样的效果:

@Schema({
  timestamps: true,
})

时间戳选项告诉猫鼬将createdAt和updatedAt字段分配给你的模式。分配的类型是Date。

默认情况下,字段的名称是createdAt和updatedAt。

通过设置时间戳自定义字段名。createdAt和timestamp . updatedat。

您可以使用中间件和虚拟设备。下面是updated_at字段的示例:

ItemSchema.virtual('name').set(function (name) {
  this.updated_at = Date.now;
  return name;
});