如何在nodejs和mongodb中发布日期对象

时间:2016-07-07 10:30:21

标签: mongodb mongoose

以下是我的架构定义:

var mongoose     = require('mongoose');
var Schema       = mongoose.Schema;

var StudentSchema   = new Schema({
    name: String,
    family: String,
    created: {
    type: Date,
    default: Date.now}
});

module.exports = mongoose.model('Student', StudentSchema);

我如何发布?因为我发布的任何值,它的默认值,即当前时间。我尝试过两件事:

{
    "name": "abc",
    "family": "xyz",
    "created": "2016-12-12"
}

{
    "name": "abc",
    "family": "xyz",
    "created": "1467883104"
}

在这两种情况下,它仅存储当前时间。不是我要经过的那个。

1 个答案:

答案 0 :(得分:3)

您的架构将字段created定义为Date对象。但是,您要存储的是一个纯字符串。你必须传递一个真实的Date object

{
    name: "abc",
    family: "xyz",
    created: new Date("2016-12-12")
}