如何在数据迁移期间使用 mongoose 设置 updatedAt 时间戳

编程入门 行业动态 更新时间:2024-10-11 17:21:13
本文介绍了如何在数据迁移期间使用 mongoose 设置 updatedAt 时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在将数据从 MS SQL 迁移到 MongoDB.我正在使用猫鼬,并在我的架构中将时间戳属性设置为 true.

I am doing a data migration from MS SQL to MongoDB. I am using mongoose and in my schema I set the timestamp property to true.

{ timestamps: true }

然后我尝试设置 createdAt 和 updatedAt 字段的值.插入记录时.createdAt 字段保存正确,但是,updatedAt 字段设置为 createdAt 字段的任何值.

I then try and set the values of the createdAt and updatedAt fields. When inserting a record. The createdAt field saves correctly, however, the updatedAt field is set to whatever the createdAt field is.

这是标准行为还是我做错了什么?

Is this the standard behaviour or am I doing something wrong?

推荐答案

时间戳选项真的很酷,毫无疑问,但我仍然在做老派":

The timestamps option is really cool, without doubt, but i'm still doing it "old school":

'use strict'; /** * Module dependencies */ const mongoose = require('mongoose'); var DataSchema = new mongoose.Schema({ name: { type: String, required: true, lowercase: true }, created: { type: Date, default: Date.now }, updated: { type: Date, default: Date.now } }); DataSchema.pre('save', function(next) { this.updated = Date.now(); return next(); }); mongoose.model('Data', DataSchema);

更多推荐

如何在数据迁移期间使用 mongoose 设置 updatedAt 时间戳

本文发布于:2023-11-24 11:46:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1625074.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时间   数据   如何在   mongoose   updatedAt

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!