How to use timestamp in Firebase?

HI,

I use Go to work with my Firebase instance. I inserted a document like this.

firestoreClient.Collection(t.CacheName).Doc(id).Set(t.Context, DataObject{
Id: id,
Url: request.URL.RequestURI(),
ExpireAt: time.Now().Add(time.Hour * 48),
})

In the Firebase web portal, I can see the document and the "ExpireAt" field is correctly recognized as a "timestamp".

But when I try to query the documents using the "ExpireAt" field from the web portal, no document returns. I have tried to use a string and a number to query against that field.

Solved Solved
0 4 1,894
1 ACCEPTED SOLUTION

When you try to use a WHERE clause and use the “==” (or “!=”) operator, you don’t get a “timestamp” type available;

comaro_0-1670449475585.png

…however, if you change to any other operator, you “unlock” the timestamp type.

comaro_1-1670449475571.png

The Cloud Console shows the same behavior so I can only guess this is expected.

Apparently, you cannot query documents with a field that is exactly (or not exactly) a specific timestamp, however, you can only query documents that have a value greater than, greater than or equal to, less than, etc… a specific timestamp.

For some reason, using a double condition to have the field less than or equal to AND greater than or equal to with the exact timestamp (to emulate the == operator) does not work: 

comaro_2-1670449475596.png

… subtracting a millisecond from the greater than or equal to condition (>=) just makes it work.

comaro_3-1670449475558.png

In any case, your problem seems to be the first thing I mentioned. I’d suggest you change the operator to any other than “==” or “!=” to “unlock” the timestamp type in the Query Builder.

I’d also suggest that if you’re trying to query a document with a specific timestamp, “wrap” the timestamp between “<” and “>” operators to make it work.

View solution in original post

4 REPLIES 4

Hi, David,

Could you please provide me with a minimal reproducible example?

First, I want to know how to query by "timestamp" on the Firestore web portal. The portal only allows querying by "string" and "number". If I want to query a field with the type "timestamp", is it simply impossible, or do I need to convert the "timestamp" to a number or a string, and by what rule do I perform that conversion?

Thanks

When you try to use a WHERE clause and use the “==” (or “!=”) operator, you don’t get a “timestamp” type available;

comaro_0-1670449475585.png

…however, if you change to any other operator, you “unlock” the timestamp type.

comaro_1-1670449475571.png

The Cloud Console shows the same behavior so I can only guess this is expected.

Apparently, you cannot query documents with a field that is exactly (or not exactly) a specific timestamp, however, you can only query documents that have a value greater than, greater than or equal to, less than, etc… a specific timestamp.

For some reason, using a double condition to have the field less than or equal to AND greater than or equal to with the exact timestamp (to emulate the == operator) does not work: 

comaro_2-1670449475596.png

… subtracting a millisecond from the greater than or equal to condition (>=) just makes it work.

comaro_3-1670449475558.png

In any case, your problem seems to be the first thing I mentioned. I’d suggest you change the operator to any other than “==” or “!=” to “unlock” the timestamp type in the Query Builder.

I’d also suggest that if you’re trying to query a document with a specific timestamp, “wrap” the timestamp between “<” and “>” operators to make it work.

davidshen84_0-1670496404821.png

So, querying with a "timestamp" is unavailable on the filter panel from the "Panel View". I think it is okay. At least, there's one way to query with a timestamp. Thanks.

 

Top Labels in this Space