元エンジニアPMのプロダクトマネージャーお役立ち情報

スタートアップから大規模プロダクトまで担当している元エンジニアの筆者が、事業開発・プロダクトマネジメントに役立つ情報を発信します

FirestoreのGeoFireStoreで、任意の値をwhereで絞るクエリでハマった件

GeoFireStoreのMap型をwhereで絞るクエリで躓いたので、メモ。(30分くらいハマった)

やりたかったこと

GeoFireStoreで、別途保持している任意の値をwhereで絞り込みたい。 https://geofirestore.com/index.html

interface GeoDocument {
    g: string;
    l: GeoPoint;
    d: DocumentData;
  }

上記のような型指定されており、下記のようにdの中に任意の値を入れられます。この時dはマップ型。

sample = {
    ...
    d: { 
       id : 'hogehoge',
       coordinates: [35......° N, 139...° E]
    };
  }

なので、firestore公式サンプルに乗っているマップ型の場合を参考にwhere句で絞り込む

  const geofirestore: GeoFirestore = new GeoFirestore(firestore);
  const geocollection: GeoCollectionReference = geofirestore.collection('geo');
  const ref = geocollection.where("d.id", '==', id).get();

これだとなぜか取れない。 ちなみに通常のマップ型は以下のようにクエリを書くことで解決します。

 const ref = fireStore.collection('doc').where('test.is_map','==',true).get();

解決方法=> dは記入せず、直接マップ以下のキー名で絞り込む。

  const geofirestore: GeoFirestore = new GeoFirestore(firestore);
  const geocollection: GeoCollectionReference = geofirestore.collection('geo');
  const ref = geocollection.where("id", '==', id).get();

理由がイマイチピンときていないのですが、とりあえずハマったのでメモ..