2017-02-23 52 views
0
"vehicles" : { 
      "-KbwlIGLm6dxffPJoJJB" : {    
      "fuel" : "petrol", 
      "groups" : { 
       "-KdWh_9KVF16efcSdrji" : true, 
       "-Kdb1G720MDgbuR3nPBL" : true 
      }, 
      "make" : "Honda", 
      "model" : "City", 
      "name" : "Honda City",    
      "speed_limit" : 100, 
      "tank_size" : 32, 
      "type" : "car" 
      }, 
      "-KdU-BlfEdqzKxFjGI3D" : {    
      "fuel" : "petrol", 
      "groups" : { 
       "-KdWh_9KVF16efcSdrji" : true 
      }, 
      "make" : "yamaha", 
      "model" : "FZ", 
      "name" : "Yamaza FZ",    
      "speed_limit" : 60, 
      "tank_size" : 12, 
      "type" : "bike" 
      }   
     } 

我想检索结果,其中groups-KdWh_9KVF16efcSdrji,且密钥必须等于true。如何根据布尔值查询Firebase?

vehicles_ref.child("groups").orderByChild("-KdWh_9KVF16efcSdrji").equalTo(true).on 
("value", function (snapshot) { 
    console.log(snapshot.val()); 
}); 

但是目前我得到NULL为上述标准。

我已经改变查询

vehicles_ref.orderByChild("/groups/-KdWh_9KVF16efcSdrji").equalTo(true).on("value", function (snapshot) { 
    console.log(snapshot.val()); 
}); 

现在取得成果,但现在越来越警告

火力警告:使用未指定索引。考虑加入“.indexOn”:‘群/ -KdWh_9KVF16efcSdrji’在/车辆安全规则获得更好的性能

如何添加索引以消除此警告?

+0

你试图把车辆进行分类。虽然您可以为其添加索引,但这意味着您需要为每个类别添加一个新索引。相反,看看我的答案在这里为更好的方式建模类别:http://stackoverflow.com/questions/40656589/firebase-query-if-child-of-child-contains-a-value –

+0

我们正在运行一个带有此警告的大型系统。我们做得很好。这不是一个错误。 –

回答

0

我也有过这样的警告,我发送了Firebase,并且在查询查找具有相同ID的项目的查询中过滤查询。警告信息“.indexOn”是指在数据库中的规则,这应该加入“.indexOn”你正在试图将项目中的排序组进行检查。

{ 

    "scores": { 

    "bruhathkayosaurus" : 55, 
    "lambeosaurus" : 21, 
    "linhenykus" : 80, 
    "pterodactyl" : 93, 
    "stegosaurus" : 5, 
    "triceratops" : 22 
     } 
} 

因为恐龙的名字只是关键。 我们可以添加一个.value的规则,我们的节点/分,以优化我们的查询:

{ 

    "rules": { 

    "scores": { 
     ".indexOn": ".value" 
     } 
     } 
} 

您可以使用下面的链接: https://firebase.google.com/docs/database/security/indexing-data?hl=es-419