Get all documents

Get all documents within a Firestore Collection

Documentation pages that include this code sample

To view the code sample used in context, see the following documentation:

Code sample

Go

fmt.Println("All cities:")
iter := client.Collection("cities").Documents(ctx)
for {
	doc, err := iter.Next()
	if err == iterator.Done {
		break
	}
	if err != nil {
		return err
	}
	fmt.Println(doc.Data())
}

Java

//asynchronously retrieve all documents
ApiFuture<QuerySnapshot> future = db.collection("cities").get();
// future.get() blocks on response
List<QueryDocumentSnapshot> documents = future.get().getDocuments();
for (QueryDocumentSnapshot document : documents) {
  System.out.println(document.getId() + " => " + document.toObject(City.class));
}

Node.js

const citiesRef = db.collection('cities');
const snapshot = await citiesRef.get();
snapshot.forEach(doc => {
  console.log(doc.id, '=>', doc.data());
});

PHP

$citiesRef = $db->collection('samples/php/cities');
$documents = $citiesRef->documents();
foreach ($documents as $document) {
    if ($document->exists()) {
        printf('Document data for document %s:' . PHP_EOL, $document->id());
        print_r($document->data());
        printf(PHP_EOL);
    } else {
        printf('Document %s does not exist!' . PHP_EOL, $document->id());
    }
}

Python

docs = db.collection(u'cities').stream()

for doc in docs:
    print(f'{doc.id} => {doc.to_dict()}')

Ruby

cities_ref = firestore.col collection_path
cities_ref.get do |city|
  puts "#{city.document_id} data: #{city.data}."
end

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.