我的Firestore模型就像
{ 食物:[ “黄油,奶酪”:{ “能量-大卡”:122, “碳水化合物-g”:33, “脂肪-g”:23 }, 。 。 ] }
let query = db.collection("foods");
let list = ["Energy - kcal","Carbohydrate - g"];
for(let i=0;i<list.length;i+=1) {
let each = list[i];
switch (each) {
case "Energy - kcal":
query = query.orderBy("Energy - kcal","desc");
break;
case "Carbohydrate - g":
query = query.orderBy("Carbohydrate - g","desc");
break;
case "Cholesterol - g":
query = query.orderBy("Cholesterol - g","desc");
break;
case "Fat - g":
query = query.orderBy("Fat - g","desc");
break;
case "Protein - g":
query = query.orderBy("Protein - g","desc");
break;
case "Fatty acids - g":
query = query.orderBy("Fatty acids - g","desc");
break;
case "Sodium - mmg":
query = query.orderBy("Sodium - mmg","desc");
break;
case "Potassium - mmg":
query = query.orderBy("Potassium - mmg","desc");
break;
case "sugar":
query = query.orderBy("sugar","desc");
break;
case "fiber":
query = query.orderBy("fiber","desc");
break;
case "calcium":
query = query.orderBy("calcium","desc");
break;
case "iron":
query = query.orderBy("iron","desc");
break;
case "vitamin_a":
query = query.orderBy("vitamin_a","desc");
break;
case "vitamin_c":
query = query.orderBy("vitamin_c","desc");
break;
}
}
错误:9 FAILED_PRECONDITION:查询需要索引
用于标识索引中使用哪些字段的正确方法应该如何使用?如何利用Cloud Firestore的索引合并功能?
谢谢
答案 0 :(得分:0)
仅当在查询中使用多个(链接的)where()
函数调用时,才可以利用索引合并。由于您的查询将始终基于该where()
中的单个值进行一个单个 list
调用以及一个orderBy()
调用,因此您无法合并任何内容。因此,您需要为每个属性分别创建一个索引。
编辑:
根据您的评论,如果您需要根据14个属性分别查询数据并同时对其进行排序,那么这就是解决方案。这不是一个好东西,但这就是你所拥有的。对于您的情况,建议您使用Firebase CLI部署索引。