Flutter firebase 쿼리, 하위 컬렉션 존재를 기반으로 필터링

Nov 15 2020

요청한 요리를 기반으로 레스토랑 결과를 필터링하는 Flutter에서 Firestore 쿼리를 작성하려고합니다.

지도 에 Lucas Aschenbach의 문자열 이 포함 된 Firestore 쿼리 의 답변 에 따라 요리가 공통 하위 컬렉션에 나열되도록 데이터베이스를 설계했습니다.

내 쿼리는 다음과 같습니다.

final Query locations = FirebaseFirestore.instance
        .collection('locations')     
        .where('cuisines.$id.exists', isEqualTo: true);

하위 컬렉션에 대한 읽기 권한을 추가했습니다.

match /locations/{l} {
      allow read: if true;
      allow write: if request.auth.token.role == 'admin';
    }
match /locations/{l}/cuisines {
      allow read: if true;
      allow write: if request.auth.token.role == 'admin';
    }

내 테스트 케이스의 $ id는 "미국인"입니다.

...하지만 ... 결과가 없습니다.

누구든지 분명히 잘못된 것을 본 적이 있습니까? 감사합니다!

답변

1 FrankvanPuffelen Nov 15 2020 at 07:10

Cloud Firestore의 쿼리는 단일 컬렉션 또는 동일한 이름의 컬렉션 그룹에서 작동합니다. 쿼리에서 다른 컬렉션의 데이터를 조건 (또는 읽기) 할 방법이 없습니다.

즉 , 레스토랑 문서에있는 .where('cuisines.$id.exists', isEqualTo: true)경우에만 작업 할 수 있습니다 cuisines.$id.exists. 유스 케이스를 구현하는 가장 일반적인 방법 servedCuisines은 레스토랑 문서에 배열 필드 를 추가하는 것 입니다. 그 내용은 레스토랑에서 제공하는 요리입니다.

`servedCuisines: ["american"]`

그런 다음 arrayContains쿼리에서 작업 을 사용 합니다.

final Query locations = FirebaseFirestore.instance
        .collection('locations')     
        .where("cuisinesServed", arrayContains: id);