Query a Firestore collection with a string eq filter

Query a Firestore collection with a string eq filter

Documentation pages that include this code sample

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

Code sample

Go

query := client.Collection("cities").Where("state", "==", "CA")

Java

// Create a reference to the cities collection
CollectionReference cities = db.collection("cities");
// Create a query against the collection.
Query query = cities.whereEqualTo("state", "CA");
// retrieve  query results asynchronously using query.get()
ApiFuture<QuerySnapshot> querySnapshot = query.get();

for (DocumentSnapshot document : querySnapshot.get().getDocuments()) {
  System.out.println(document.getId());
}

Node.js

// Create a reference to the cities collection
const citiesRef = db.collection('cities');

// Create a query against the collection
const queryRef = citiesRef.where('state', '==', 'CA');

PHP

$citiesRef = $db->collection('samples/php/cities');
$query = $citiesRef->where('state', '=', 'CA');
$snapshot = $query->documents();
foreach ($snapshot as $document) {
    printf('Document %s returned by query state=CA' . PHP_EOL, $document->id());
}

Python

# Create a reference to the cities collection
cities_ref = db.collection(u'cities')

# Create a query against the collection
query_ref = cities_ref.where(u'state', u'==', u'CA')

Ruby

cities_ref = firestore.col collection_path

query = cities_ref.where "state", "=", "CA"

query.get do |city|
  puts "Document #{city.document_id} returned by query state=CA."
end

What's next

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