#how do i filter a query by documents created in the last 2 hours?

1 messages · Page 1 of 1 (latest)

latent jay
#

Hi there everyone, i am querying a table for data and i want to only collect documents created in the last 2 hours. please how do go about it?

carmine swan
#

Compare _creationTime to current time:

  const isRecent = Date.now() - new Date(_creationTime).getTime() <= 2 * 60 * 1000
#

oh duh you need to index

leaden forge
#

You can also do this using the built-in index:

  const cutoff = Date.now() - 2 * 60 * 1000;
  return await ctx.db
    .query("messages")
    .withIndex("by_creation_time", (q) => q.gt("_creationTime", cutoff))
    .collect();
carmine swan
#

Ah so there is a built-in for that, I was wondering