Mongoose 时间戳模式选项是否已建立索引?

问题描述 投票:0回答:2

Mongoose 版本 >= 4.0 有一个时间戳选项,当

updatedAt
设置为
createdAt
时,该选项会为模式创建
timestamps
true
字段。

http://mongoosejs.com/docs/guide.html#timestamps

updatedAt
createdAt
字段是否已建立索引?

mongoose mongoose-schema
2个回答
33
投票

不,它们没有索引,您必须自己建立索引,就像任何其他字段一样。

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

0
投票

请注意,从 2023 年开始,您可以将其传递到 Schema 构造函数的第二个参数中:

{
  timestamps: true, 
  index: { 'createdAt': 1, 'updatedAt': 1 },
}
© www.soinside.com 2019 - 2024. All rights reserved.