猫鼬中的 Date.now() 和 Date.now 有什么区别? [英] What is the difference between Date.now() and Date.now in mongoose?
问题描述
我在猫鼬模型中使用 Date.now()
和 Date.now
.
I am using Date.now()
and Date.now
in mongoose model.
我对它们之间的区别有点困惑.你能帮我吗?
I am a little bit confused about the difference between them. Could you please help me?
推荐答案
Date.now
可用于您的 Mongoose 架构定义中以设置字段的默认值,而 Date.now()
是等效的 JavaScript.例如,在架构定义中设置默认值时,您可以使用 Date.now
.使用此架构定义,Mongoose 将使用当前时间填充 createdDate
.
Date.now
can be used in your Mongoose schema definition to set a default value for a field, whereas Date.now()
is the JavaScript equivalent. For example, when setting a default value in your schema definition, you use Date.now
. With this schema definition, Mongoose will populated createdDate
with the current time.
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
//schema
var yourSchema= new Schema({
text: {type: String},
createdAt: {type: Date, default: Date.now}
});
但是,当针对您的架构编写 JavaScript 代码时,您必须使用 Date.now()
However, when writing JavaScript code against your schema, you have to use Date.now()
yourSchema.pre('save', function doSomething(next){
var something = this;
something.createdAt(Date.now());
next();
});
这篇关于猫鼬中的 Date.now() 和 Date.now 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!