I created a Firestore Database all fields are in String Format.
Now I need to by date on this field (reqDate) which is in string format.
Sample Data on the field (reqDate) = 10/11/2021, 9:22:11 PM
I tried the code below on Cloud Functions using moment to format the string field:
const thisCollection = firestore.collection("ItemList")
.where(moment("reqDate").format('MM/DD/YYYY'), ">", "10/05/2021");
But it return blank or no record.
Thanks in advance
CodePudding user response:
First off, the condition you pass to you query is wrong because you need to pass:
- The field name to filter on in the first parameter, which is now
moment("reqDate").format('MM/DD/YYYY')
, but should be"reqDate"
. - The operator, which is correct in your code.
- The value to filter on.
This date format you use is unsuitable for range filters, since strings are ordered lexicographically and in that order: "10/05/2021"
is before "11/05/2020"
.
To allow a range query, you need a date format that allows for that. When the date is a string, the most common formats are based on ISO-8601, such as "2021-10-17"
, because in this format dates that are after October 17 are queryable with: .where("reqDate", ">", "2021-10-17")