NerdkidChiki
NerdkidChiki15mo ago

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

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?
4 Replies
erquhart
erquhart15mo ago
Compare _creationTime to current time:
const isRecent = Date.now() - new Date(_creationTime).getTime() <= 2 * 60 * 1000
const isRecent = Date.now() - new Date(_creationTime).getTime() <= 2 * 60 * 1000
oh duh you need to index Some examples here: https://docs.convex.dev/database/indexes/#querying-documents-using-indexes
Michal Srb
Michal Srb15mo ago
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();
const cutoff = Date.now() - 2 * 60 * 1000;
return await ctx.db
.query("messages")
.withIndex("by_creation_time", (q) => q.gt("_creationTime", cutoff))
.collect();
erquhart
erquhart15mo ago
Ah so there is a built-in for that, I was wondering
NerdkidChiki
NerdkidChikiOP15mo ago
Thanks so much

Did you find this page helpful?