#Correct way to create compound MongoDB indexes via a migration

2 messages · Page 1 of 1 (latest)

mortal quartz
#

I currently have a migration which looks like this:


export async function up({ payload }: MigrateUpArgs): Promise<void> {
  payload.db.collections['unique-codes'].schema.index({ uniqueCode: 1, offer: 1, createdAt: -1 })

  payload.db.collections['offer-redemptions'].schema.index({ offer: 1, blockId: 1, userId: 1 })
  payload.db.collections['offer-redemptions'].schema.index({ offer: 1, blockId: 1, blockType: 1 })
  payload.db.collections['offer-redemptions'].schema.index({
    offer: 1,
    blockId: 1,
    blockType: 1,
    userId: 1,
    parent: 1,
  })

  payload.db.collections['offers'].schema.index({
    id: 1,
    state: 1,
    publishDate: 1,
    expiryDate: 1,
    hidden: 1,
    displayDate: -1,
  })
  payload.db.collections['offers'].schema.index({
    offerType: 1,
    state: 1,
    closingDate: 1,
    'competitionWinners.name': 1,
    displayDate: -1,
  })
}

But this is not creating any indexes. This feels like an extremely common task to want to complete yet I can't find much about it in the docs, GitHub or Discord.