여러 필드에 범위 및 불일치 필터가 있는 복합 쿼리

Firestore 문서 계획에서 원래 요청된 Firestore 및 Datastore 예시와 일치하는 C# 및 Ruby 샘플을 제공해 주세요(GA 및 버그 351980346의 여러 불일치). 요청은 turbo/469184에 관한 것입니다.

더 살펴보기

이 코드 샘플이 포함된 자세한 문서는 다음을 참조하세요.

코드 샘플

C#

Firestore에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다. 자세한 내용은 로컬 개발 환경의 인증 설정을 참조하세요.

CollectionReference citiesRef = db.Collection("cities");
Query query = citiesRef
    .WhereGreaterThan("Population", 1000000)
    .WhereLessThan("Density", 10000);
QuerySnapshot querySnapshot = await query.GetSnapshotAsync();
foreach (DocumentSnapshot documentSnapshot in querySnapshot)
{
    var name = documentSnapshot.GetValue<string>("Name");
    var population = documentSnapshot.GetValue<int>("Population");
    var density = documentSnapshot.GetValue<int>("Density");
    Console.WriteLine($"City '{name}' returned by query. Population={population}; Density={density}");
}

Go

Firestore에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다. 자세한 내용은 로컬 개발 환경의 인증 설정을 참조하세요.

import (
	"context"
	"fmt"
	"io"

	"cloud.google.com/go/firestore"
)

func multipleInequalitiesQuery(w io.Writer, projectID string) error {
	ctx := context.Background()

	// Create client
	client, err := firestore.NewClient(ctx, projectID)
	if err != nil {
		return fmt.Errorf("firestore.NewClient: %w", err)
	}
	defer client.Close()

	// Create query
	query := client.Collection("cities").
		Where("population", ">", 1000000).
		Where("density", "<", 10000)

	// Get documents
	docSnapshots, err := query.Documents(ctx).GetAll()
	for _, doc := range docSnapshots {
		fmt.Fprintln(w, doc.Data())
	}
	if err != nil {
		return fmt.Errorf("GetAll: %w", err)
	}

	return nil
}

Java

Firestore에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다. 자세한 내용은 로컬 개발 환경의 인증 설정을 참조하세요.

Query query =
    db.collection("cities")
        .whereGreaterThan("population", 1000000)
        .whereLessThan("density", 10000);

PHP

Firestore에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다. 자세한 내용은 로컬 개발 환경의 인증 설정을 참조하세요.

$collection = $db->collection('samples/php/cities');
$chainedQuery = $collection
    ->where('population', '>', 1000000)
    ->where('density', '<', 10000);

Python

Firestore에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다. 자세한 내용은 로컬 개발 환경의 인증 설정을 참조하세요.

query = (
    db.collection("cities")
    .where(filter=FieldFilter("population", ">", 1_000_000))
    .where(filter=FieldFilter("density", "<", 10_000))
)

Ruby

Firestore에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다. 자세한 내용은 로컬 개발 환경의 인증 설정을 참조하세요.

cities_ref = firestore.col collection_path
compound_multi_ineq_query = cities_ref.where("population", ">", 1_000_000).where("density", "<", 5_000)

다음 단계

다른 Google Cloud 제품의 코드 샘플을 검색하고 필터링하려면 Google Cloud 샘플 브라우저 참조하기