Query Firestore Database for Document Id

Query firestore database for document id

Try this:


(The first query is looking for an explicit user-set field called 'id', which probably isn't what you want.)

Firestore query: How to filter on document id?

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

Firebase Firestore query by document id using swift

To filter on the document ID in a query, you can use FieldPath.documentID() in Swift too: https://firebase.google.com/docs/reference/swift/firebasefirestore/api/reference/Classes/FieldPath#/c:objc(cs)FIRFieldPath(cm)documentID. But if you do this in a single document, that'd be the same as just doing document(User.id).

Get Document ID from Firebase Query using Flutter

A query can return multiple documents that match the given condition and returns a QuerySnapshot (that contains zero or many DocumentSnapshots).

.where('uid', isEqualTo: FirebaseAuth.instance.currentUser!.uid)
.then((value) {
value.docs.forEach((element) {

Here value.docs is a list of all the documents included in this snapshot and each of them has id property (the document ID).

Firebase query by document id PLUS property value

If you want to get a document only if a field has a certain value, you won't be able to use get() on its DocumentReference. You will have to perform a query on the collection using both the document ID and field as filters on that query like this, using FieldPath.documentId():

const qsnap = await firebase.firestore()
.where(firebase.firestore.FieldPath.documentId(), "==", id)
.where("published", "==" true)

This yields a QuerySnapshot, which you will have to check if the document was provided.

if (qsnap.docs.length > 0) {
const doc = qsnap.docs[0];

Note that it still costs 1 document read to perform this query even if the document isn't returned.

Flutter-Firestore: get Document with know id from Collection Group Query

You can use collectionGroup() query with a where() clause.

Firestore.instance.collectionGroup('users').where('userIdField', isEqualTo: 'USER_ID')

You can read the user's company ID from the DocumentReference of that fetched document.

As @Frank commented, using where with document ID in a collection group query will not match. You would have to store user's UID as a field as well.

How to get document ID for a specific document?

First, create a variable called townid, and change your function to async, and use a stateful widget to update it, and use get instead of snapshots:

String townId = 'Get ID';

void _getId(town) async {
var data = await FirebaseFirestore.instance
.where('town_name', isEqualTo: town)
setState(() {
townId = data.docs[0].id; //because the query returns a list of docs, even if the result is 1 document. You need to access it using index[0].

In your button:

ElevatedButton(onPressed: () => _getId(_townChosen), child: Text(townId)),

Related Topics

Leave a reply