Search code examples
firebasegoogle-cloud-firestore

Is there a method to iterate through all documents in a collection in firestore


I'm using the firestore of firebase and I want to iterate through the whole collection. Is there something like:

db.collection('something').forEach((doc) => {
  // do something
})

Solution

  • Yes, you can simply query the collection for all its documents using the get() method on the collection reference. A CollectionReference object subclasses Query, so you can call Query methods on it. By itself, a collection reference is essentially an unfiltered query for all of its documents.

    Android: Query.get()

    iOS/Swift: Query.getDocuments()

    JavaScript: Query.get()

    In each platform, this method is asynchronous, so you'll have to deal with the callbacks correctly.

    See also the product documentation for "Get all documents in a collection".