How to query collections in Firestore under a certain path?

A simple solution to get content across different sub-collections

Alex Mamo
Firebase Developers

--

Cloud Firestore is a flexible, scalable database for mobile, web, and server development from Firebase and Google Cloud. It consists of collections and documents. When working with such a database, there often are cases in which we would like to query multiple collections at once. But unfortunately, this is not possible. And it makes sense since the queries in Firestore are shallow, meaning that can only get documents from the collection that the query is run against. There is no way to get documents from different collections or sub-collections in a single query. Firestore doesn’t support queries across different collections in one go unless we are using a collection group query.

But how about limiting the results only to sub-collections that exist under a particular document? So I’m going to explain in this article, the most simple and efficient way for solving this problem.

Let’s assume we have an application that displays landmarks from all over the world, from the smallest to the largest cities. So let’s take a simple example of a Firestore Database structure for such an app:

Firestore-root
|
--- countries (collection)
|
--- UK…

--

--