NoNam4
NoNam4

Reputation: 1805

Wildcard queries for documents in Firestore subcollections

my Firestore database is organized as follows: (col = collection, doc = document)

-data (col)
  -userid -> random number (doc)
    -clients (col)
      -clientid -> random number (doc)
      -clientid -> random number (doc)
  -userid(doc)
    -clients (col)
      -clientid -> random number (doc) 
      ...

and I need to read all data inside a specific "clientid" document just asking the user to insert it's clientid random number.

I wonder if I can read this data with wildcard syntax like in the rules of database:

var clientid = "65486649466"
var clientdata = db.collection("data").doc({userid}).collection("clients").doc(clientid);

Or there is another way of doing this? Thanks

Upvotes: 3

Views: 1343

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317497

There are no wildcards in Firestore client queries. You need to provide the exact values for all collection and document IDs needed to make the query.

If you need to query across all subcollections called "clients" in the entire database (including those nested under "data", and elsewhere), you can instead use a collection group query to find those documents:

const query = db.collectionGroup("clients")
query.get()...

This query will return all the documents among all the subcollections named "clients". Unfortunately, you can't filter this query based on the document ID of each client document. What you would have to do for this is also write the client document ID as a field in each document. Suppose you've done that, and the field name is called "id":

const query = db.collectionGroup("clients").where("id", "==", clientid)
query.get()...

This will give you all the "clients" subcollection documents where the "id" field is clientid.

Upvotes: 5

Related Questions