Mongoose 版本 >= 4.0 有一个时间戳选项,当
updatedAt
设置为 createdAt
时,该选项会为模式创建 timestamps
和 true
字段。
http://mongoosejs.com/docs/guide.html#timestamps
updatedAt
和 createdAt
字段是否已建立索引?
不,它们没有索引,您必须自己建立索引,就像任何其他字段一样。
animalSchema.index({"createdAt": 1});
animalSchema.index({"updatedAt": 1});
请注意,从 2023 年开始,您可以将其传递到 Schema 构造函数的第二个参数中:
{
timestamps: true,
index: { 'createdAt': 1, 'updatedAt': 1 },
}