collection
に対しては特に何もしなくてもindexが作成されているみたいなのでorderBy
が使える。
だけどcollectionGroup
になるとindexは自動では作られないとの事
#collection
コレクションの取得
const db = firebase.firestore()
db.collection("images").orderBy("timeStamp", "desc").limit(5).get()
これはちゃんと降順に取得できる
#collectionGroup
コレクション内にあるサブコレクションの取得
####フィールド
message: "テスト"
displayName: "testUser"
timeStamp: 2020年9月3日 14:10:46 UTC+9
db.collectionGroup("comment").orderBy("timeStamp", "desc").limit(10).get()
これを実行すると下記のエラーが出て取得できない
Uncaught (in promise) FirebaseError: The query requires a COLLECTION_GROUP_DESC index for collection comment and field timeStamp. You can create it here
__index
__がないからできませんよ
といっているらしい
#設定
firebaseコンソール>firestore>インデックス>単一フィールド
collection group scope
と書いてあるところがすべて無効になっています。
除外を追加
を選択
今回の場合だとcollectionGroup("comment")
なのでコレクションID
にcomment
フィールドのパス
はtimeStamp
を使いたいのでtimeStamp
と入力して
コレクショングループにチェックを入れてNext
昇順
と降順
を使用したいので有効
にして保存
これで先ほどと同じ
db.collectionGroup("comment").orderBy("timeStamp", "desc").limit(10).get()
を実行してみます。
ちゃんと降順で取得することができました。