【问题标题】:Indexing mongoose timestamps索引猫鼬时间戳
【发布时间】: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 时间,但最好我更愿意索引内置时间戳提供的字段。

【问题讨论】:

    标签: mongodb mongoose


    【解决方案1】:

    结果

     PictureSchema.index({"createdAt": 1});
     PictureSchema.index({"updatedAt": 1});
    

    完美运行。必须重新启动数据库服务器几次才能使其正常工作。

    想知道这是为什么?

    【讨论】:

      猜你喜欢
      • 2017-01-07
      • 1970-01-01
      • 2019-04-08
      • 1970-01-01
      • 1970-01-01
      • 2020-08-20
      • 2020-10-03
      • 2016-10-29
      • 1970-01-01
      相关资源
      最近更新 更多