user11229655
user11229655

Reputation: 248

Firestore How to retrieve subcollection

I am working on App that uses Firestore please see the following pic how I organize the data

enter image description here

I am trying to get data using the following code

let db = admin.firestore();

async function getMenu() {
  let query = await db.collection('menu').orderBy("order", "asc").get();

  const snapshot = query.docs;
  snapshot.forEach((doc) => {
  console.log(doc.id, '->', doc.data());
});
}

getMenu();

Output:

Feedings -> { order: 1 }

Diapers -> { order: 2 }

Can't able to get subcollection

Diapers & Wipes -> Disposable Diapers

Any Help will be appreciated.

Upvotes: 0

Views: 48

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317352

It's not possible to get data from a subcollection using a query for a top-level collection. When you query menu, it will only ever give you document immediately within that collection. In this respect, Firestore queries are said to be "shallow".

If you want documents from a subcollection, you will need to make another query that targets that specific subcollection. For example:

db.collection("menu").doc("Diapers").collection("Diapers & Wipes").get()

Upvotes: 1

Related Questions