Firestore - subcollection query for a specific document

Firebase now supports querying subcollections (as of May 2019).

You can do this

db.collectionGroup("lists").where("public", "==", true)

which will return matching lists documents for all users.


The SDK now supports this

Please see the other answers below...

Old answer

You can query on a subcollection as long as you know the document it belongs to. For example the following is a valid subcollection query:

const usersCollection = firestore.collection("users");
const adminDocument = usersCollection.doc("admin");
const adminsFollowersQuery = adminDocument.collection("followers").where("name", "==", "Arthur Dent");

adminsFollowersQuery.get().then((adminsFollowers) => {
  adminsFollowers.docs.forEach((adminFollower) => {
    console.log(adminFollower.get("name"));
  });
});

As you point out the following is NOT currently valid:

const allUserFollowersQuery = firestore.collection("users/{wildcard}/followers")
  .where("name", "==", "Arthur Dent");

adminsFollowersQuery.get().then((allUserFollowers) => {
  allUserFollowers.docs.forEach((follower) => {
    console.log(follower.get("name"));
  });
});

I think the documentation here is a little confusing but by "Querying across subcollections" they mean, you can query a specific subcollection, as above, but you cannot query general subcollections. At least not as a single action using the FireStore SDK.