【发布时间】:2017-09-04 09:53:47
【问题描述】:
我有一个像这样的猫鼬模式:
let PictureSchema = mongoose.Schema({
...
...
}, {timestamps: true})
PictureSchema.index({"createdAt": 1});
PictureSchema.index({"updatedAt": 1});
我正在尝试将字段“createdAt”和“updatedAt”编入索引。使用时
PictureSchema.index({"createdAt": 1});
PictureSchema.index({"updatedAt": 1});
它不起作用,除“_id_”之外的所有其他索引也停止工作。
我通过使用 mongoose-timestamp 插件得到了这个工作的变体,如下所示:
PictureSchema.plugin(timestamps, {
createdAt: {
name: 'createdAt',
type: Date,
index: true
},
updatedAt: {
name: 'updatedAt',
type: Date,
index: true
}
})
但是我对这个插件的问题是它不记录UTC时间而是系统时间。解决我的问题的方法也是让 mongoose-timestamp 记录 UTC 时间,但最好我更愿意索引内置时间戳提供的字段。
【问题讨论】: