Gcp/Firebase/Firestore/複合クエリ
提供: 初心者エンジニアの簡易メモ
複合クエリとは
二つ条件があると複合クエリとなる。
複合クエリはインデックスを作らないければならない。
サンプル
admin.firestore().collection('caches')
.where('id', '==', id)
.where('unixtime', '>', unixtimecachelifesec)
.limit(1)
.get()
.then((snapshot) =>
{
});
インデッックス作成
コレクションidをcachesにしてidをascではじめに作りunixtimeをascで作れば良い。 whereの順序(idとunixtimeの順序)が逆だとインデックスが無効となりインデックスを作れと警告がでる。
インデックスをコンソールで作成
まずはfirestoreのプロジェクトを持ってくる
firebase init firestore
デプロイ
firebase deploy --only firestore:indexes
インデックスだけ取得(ただしプロジェクトを持ってこないと後でデプロイはできない)
firebase firestore:indexes > firestore.indexes.json
