2016-11-16 190 views
4

我有一些数据,我试图按照Firebase关于扁平结构的建议,所以我没有比我需要的更多。最终的结果是我在节点像这样有组织的报价:使用通配符作为路径的深层路径查询

quotes -> clientName -> quoteObject 

的quoteObjects有一个'dateCreated会的价值,我希望能够拉像这样(这个数据时,我拉着一个大的所有的报价为特定页面的列表,然后我用对象分配,使对象的一个​​大阵列来显示):

const quotesRef = firebase.database().ref('quotes'); 
    quotesRef.orderByChild('dateCreated').on('value', snapshot => { 
     // function 
    }); 

不幸的是,dateCreated会值超过一层深,标准查询不工作。我知道如果你知道父路径,你可以做一个深层路径查询,但在我的情况下,clientName父节点总是唯一的。鉴于此,是否可以指定一种通配符路径?如果是的话,我会怎么做呢?干杯!

编辑:显示数据库的例子,对不起应该是最初更具体。

quotes 
    - ClientEmailOne 
     -UniqueQuoteID 
      - createdBy: ClientEmailOne 
      - dateCreated: 1479255005172 
      - email: "[email protected]" 
    - ClientEmailTwo 
     -UniqueQuoteID 
      - createdBy: ClientEmailTwo 
      - dateCreated: 1479255005172 
      - email: "[email protected]" 
    - ClientEmailThree 
     -UniqueQuoteID 
      - createdBy: ClientEmailThree 
      - dateCreated: 1479255005172 
      - email: "[email protected]" 
     -UniqueQuoteID 
      - createdBy: ClientEmailThree 
      - dateCreated: 1479255005172 
      - email: "[email protected]" 
     -UniqueQuoteID 
      - createdBy: ClientEmailThree 
      - dateCreated: 1479255005172 
      - email: "[email protected]" 

回答

3

Firebase会考虑您运行查询的位置的子女。您指定要订购/过滤的孩子可以包含一个属性的路径,但其不能包含任何通配符。

更新

随着新的数据结构,似乎你试图在所有客户端,并且使各这些客户的所有报价进行查询。这里有两个通配符(“所有客户端”和“所有引用”),Firebase的查询模型不允许。

您当前的模型允许查询所有的报价为特定客户:

ref.child("quotes").child("ClientEmailOne").orderByChild("dateCreated") 

如果您想查询所有客户端的所有报价,你需要添加一个包含所有报价的数据结构(不考虑他们的客户):

allquotes 
    -UniqueQuoteID 
     - createdBy: ClientEmailOne 
     - dateCreated: 1479255005172 
     - email: "[email protected]" 
    -UniqueQuoteID 
     - createdBy: ClientEmailTwo 
     - dateCreated: 1479255005172 
     - email: "[email protected]" 
    -UniqueQuoteID 
     - createdBy: ClientEmailThree 
     - dateCreated: 1479255005172 
     - email: "[email protected]" 
    -UniqueQuoteID 
     - createdBy: ClientEmailThree 
     - dateCreated: 1479255005172 
     - email: "[email protected]" 
    -UniqueQuoteID 
     - createdBy: ClientEmailThree 
     - dateCreated: 1479255005172 
     - email: "[email protected]" 

然后你就可以查询:

ref.child("allquotes").orderByChild("dateCreated") 
+0

感谢您的快速响应弗拉NK!所以只是为了澄清对不起,这是否意味着它应该已经按照现在的价值排序(即使它嵌套了几个深度)?或者它是不可能的?因为它回来未分类。谢谢 –

+1

Firebase可以通过嵌套的孩子*仅限于*当这些孩子在固定路径上。所以你可以订购'ref.child(“quotes”)。orderByChild(“property/under/clientName”)',但不是'ref.child(“quotes”)。orderByChild(“property/???/clientName”) ' –

+0

好的感谢澄清。因此,鉴于Firebase建议以这种方式构建数据,例如在具有各种ID密钥的单独节点下,他们又如何推荐以这种方式进行查询?这似乎是相当普遍,简单的事情,预计必须做的?还是假设您只是按照原样提取数据并对其进行排序呢? –