Mongo 仅以 ISO_8601 格式而不是 unix 格式存储日期
Mongo is only storing date in ISO_8601 format instead of unix
我正在尝试以 UNIX 格式存储在集合中,但它每次都以 ISO_8601 格式存储。
下面是我的代码,你能快速浏览一下并告诉我哪里错了吗?
var date = Date.now();
var comments = new Comments({
item_id : req.params.item_id,
content : req.body.content,
created_at: date,
updated_at : "",
is_deleted : 0
});
评论架构
var CommentsSchema = new Schema({
content: {type: String},
item_id: {type: Number},
created_at: {type: Date, default: Date.now},
updated_at: {type: Date, default: Date.now},
is_deleted: Number
});
但是当我 console.log()
comments 对象中的内容时,其显示日期为 ISO_8601 格式。我什至尝试使用 moment js
来实现相同但徒劳无功。
Mongoose v4.x has time stamping as a built-in option to a schema
var mySchema = new mongoose.Schema( {key:keyType,}, {timestamps: true} );
var CommentsSchema = new Schema({
content: {type: String},
item_id: {type: Number},
is_deleted: Number
},{timestamps: true});
This option adds createdAt
and updatedAt
properties that are timestamped
with a Date
, and which does all the work for you. Update request updates the document, it updates the updatedAt property
例子
var thingSchema = new Schema({..}, { timestamps: { createdAt: 'created_at' } });
var Thing = mongoose.model('Thing', thingSchema);
var thing = new Thing();
thing.save(); // `created_at` & `updatedAt` will be included
By default, the name of two fields are createdAt and updatedAt, custom the field name by setting timestamps.createdAt and timestamps.updatedAt.
有关详细信息,请参阅 document
如果您想在数据库中存储时间戳,您应该在架构中使用 type : Number
,而不是 type : Date
。
否则,当从数据库中读取数据时,Mongoose 会自动将时间戳转换为正确的 Date
实例(尽管您仍然可以使用 doc.created_at.getTime()
从中提取时间戳)。
我正在尝试以 UNIX 格式存储在集合中,但它每次都以 ISO_8601 格式存储。 下面是我的代码,你能快速浏览一下并告诉我哪里错了吗?
var date = Date.now();
var comments = new Comments({
item_id : req.params.item_id,
content : req.body.content,
created_at: date,
updated_at : "",
is_deleted : 0
});
评论架构
var CommentsSchema = new Schema({
content: {type: String},
item_id: {type: Number},
created_at: {type: Date, default: Date.now},
updated_at: {type: Date, default: Date.now},
is_deleted: Number
});
但是当我 console.log()
comments 对象中的内容时,其显示日期为 ISO_8601 格式。我什至尝试使用 moment js
来实现相同但徒劳无功。
Mongoose v4.x has time stamping as a built-in option to a schema
var mySchema = new mongoose.Schema( {key:keyType,}, {timestamps: true} );
var CommentsSchema = new Schema({
content: {type: String},
item_id: {type: Number},
is_deleted: Number
},{timestamps: true});
This option adds
createdAt
andupdatedAt
properties that aretimestamped
with aDate
, and which does all the work for you. Update request updates the document, it updates the updatedAt property
例子
var thingSchema = new Schema({..}, { timestamps: { createdAt: 'created_at' } });
var Thing = mongoose.model('Thing', thingSchema);
var thing = new Thing();
thing.save(); // `created_at` & `updatedAt` will be included
By default, the name of two fields are createdAt and updatedAt, custom the field name by setting timestamps.createdAt and timestamps.updatedAt.
有关详细信息,请参阅 document
如果您想在数据库中存储时间戳,您应该在架构中使用 type : Number
,而不是 type : Date
。
否则,当从数据库中读取数据时,Mongoose 会自动将时间戳转换为正确的 Date
实例(尽管您仍然可以使用 doc.created_at.getTime()
从中提取时间戳)。