我们现在可以从集合组中检索文档,这很棒。为此,我需要通过 Firebase 控制台上的错误消息创建索引。如何将此新索引添加到 firestore.indexes.json 文件?
documentation 的示例:
let museums = db.collectionGroup('landmarks').where('type', '==', 'museum');
museums.get().then(function(querySnapshot) {
querySnapshot.forEach(function(doc) {
console.log(doc.id, ' => ', doc.data());
});
});
最佳答案
在索引文件的顶层,添加一个名为 fieldOverrides
的新元素。并像这样填充它:
{
"fieldOverrides": [
{
"collectionGroup": "landmarks",
"fieldPath": "type",
"indexes": [
{
"order": "ASCENDING",
"queryScope": "COLLECTION"
},
{
"order": "DESCENDING",
"queryScope": "COLLECTION"
},
{
"arrayConfig": "CONTAINS",
"queryScope": "COLLECTION"
},
{
"order": "ASCENDING",
"queryScope": "COLLECTION_GROUP"
}
]
}
]
}
这保留了
type
的所有默认自动索引。字段在 landmarks
在 COLLECTION
范围,并允许 type
也可用于 COLLECTION_GROUP
范围。
关于firebase - 集合组查询中 firestore 单个字段索引的 JSON 格式是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56428593/