15
votes

As I got from 'Cloud Firestore Data Model' guide "each document is identified by a name." Is it possible to query a collection by that document identifier (which is the name or ID)?

For example, documents in the collection "Things" have IDs: 0, 1, 2 etc.:

enter image description here

Is it possible to query documents which IDs are less than 100?

2

2 Answers

36
votes

You can query by documentId using the special sentinel FieldPath.documentId(), e.g.:

const querySnap = collection.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.

6
votes

In python you should use full documents names

from google.cloud import firestore as f
from google.cloud.firestore_v1.field_path import FieldPath

firestore = f.Client()
colRef = firestore.collection(u'docs')
filter = [firestore.document(u'docs/doc1'), firestore.collection(u'docs/doc3')]
query = colRef.where(FieldPath.document_id(), u'in', filter)