Query a Firestore collection with an array_contains filter

Query a Firestore collection with an array_contains filter

Explore further

For detailed documentation that includes this code sample, see the following:

Code sample

C#

To authenticate to Firestore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

CollectionReference citiesRef = db.Collection("cities");
Query query = citiesRef.WhereArrayContains("Regions", "west_coast");

Go

To authenticate to Firestore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

query := cities.Where("regions", "array-contains", "west_coast").Documents(ctx)

Java

To authenticate to Firestore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

CollectionReference citiesRef = db.collection("cities");
Query westCoastQuery = citiesRef.whereArrayContains("regions", "west_coast");

Node.js

To authenticate to Firestore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

const westCoastCities = citiesRef.where('regions', 'array-contains',
  'west_coast').get();

PHP

To authenticate to Firestore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

$containsQuery = $citiesRef->where('regions', 'array-contains', 'west_coast');

Python

To authenticate to Firestore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

cities_ref = db.collection("cities")

query = cities_ref.where(
    filter=FieldFilter("regions", "array_contains", "west_coast")
)

Ruby

To authenticate to Firestore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

cities_ref = firestore.col collection_path
cities = cities_ref.where "regions", "array-contains", "west_coast"

What's next

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