I'm trying to perform a date range query to return all the records between 2 dates, but no matter what I try, it always returns zero results, even though I have the data existing in the collection.
My index was initially defined like this:
CreateIndex({
name: "entries_by_date_only",
source: Collection("Entries"),
terms: [{ field: ["data", "created"] }]
})
This is a sample record from the Entries collection:
{
"ref": Ref(Collection("Entries"), "36561645689871265"),
"ts": 1684938186510000,
"data": {
"email": "[email protected]",
"firstEntry": true,
"state": "NY",
"optin": true,
"lang": "en",
"created": "2023-08-22T10:23:06-05:00",
"name": "Test Name",
"ip": "123.123.456.456",
"eligibility": true
}
}
And this is the query I am trying:
const START_DATE = req.params.startDate; // I have tried 2023-08-01 AND 2023-08-01T00:00:00-05:00
const END_DATE = req.params.endDate; // I have tried: 2023-08-30 AND 2023-08-30T23:59:59-05:00
const entriesQuery = q.Map(
q.Paginate(
q.Range(q.Match(q.Index("entries_by_date_only")), START_DATE, END_DATE)
),
q.Lambda("entry", q.Get(q.Var("entry")))
);
const entriesResult = await client.query(entriesQuery);
console.log("ENTRIES", entriesResult); // Logs: ENTRIES { data: [] }
Is there anything I'm missing here? The field holds the string in ISO format, so I thought I could use it for this purpose. Is that even possible? Thanks in advance for any help.
The issue seems to be with the way you are constructing the date range query. The
Rangefunction in FaunaDB expects the range values to be of typeDate, but you are passing them as strings. To fix this, you need to convert the start and end dates toDatetype before passing them to theRangefunction.To convert a string to a
Datetype in FaunaDB, you can use theDatefunction.Check this out: