0

I have a collection in which I need to make queries filtering by date, so I’d like to add an index to the createdAt field (this field was created automatically when I created the collection in Strapi Content-types builder).

I already added some indexes for other fields that are working fine in /models/{name}.settings.json according to: https://forum.strapi.io/t/best-approaching-for-adding-custom-indexes/888/7

but there is no createdAt attribute in the /models/{name}.settings.json file

I've tried adding the index manually in mongo but as in the original question from the Strapi forum post Strapi drops it when reloads.

1 Answers1

0

You can define createdAt field in your schema and then index it. Defining like

const yourSchema = mongoose.Schema({
createdAt: {
type: Date,
default: Date.now()
}
})

yourSchema.index({
createdAt: 1
})
r_dmr
  • 154
  • 1
  • 7