Firestore query: How to filter on document id?

Viewed 2873

In Firestore, I have a collection containing documents identified by timestamp: /records/timestamp
I would like to filter documents based on the id, using the javascript client API.
How can I write the query?
I am looking for something along the following line, but I cannot find the proper field name for id:

const query = firestore.collection(`/records`).where("id", "<", 1600766222);
1 Answers

This is possible using FieldPath.documentId(), but there are limitations.

db.collection('CollectionName').where(firebase.firestore.FieldPath.documentId(), '<', '100').get()

But be aware that document IDs are strings and therefore this will include documents with ID '0' or '1', but not '2' since '2' > '100' lexicographically.

So if you want a numeric query, you'll need to write the document ID as a numeric field in the document and then do a normal query on it.

Reference: https://stackoverflow.com/a/48467056/1212903

Related