2015-06-22 81 views
1
檢索數據

我的火力地堡陣列的結構如下:從火力點陣列

[ 
    { 
    'userId': '12345', 
    'itemOrdered' : 'abc', 
    'status': 'pending' 
    ...other attributes 
    }, 
    { 
    'userId': '6789', 
    'itemOrdered' : 'def', 
    'status' : 'pending', 
    ...other attributes 
    }, 
    { 
    'userId': '12345', 
    'itemOrdered' : 'def', 
    'status' : 'complete', 
    ...other attributes 
    }, 

] 

我無法弄清楚如何檢索以下數據:

  1. 獲取記錄用戶id = XXX
  2. 獲取所有記錄,其中「itemOrdered」 = '高清'

火力地堡文檔談論使用orderByC但這並沒有多大意義。

+0

[避免在分佈式系統中使用數字順序標識(即數組)。](https://www.firebase.com/docs/web/guide/understanding-data.html#section-arrays-in-firebase)。真? [是的,真的](https://www.firebase.com/blog/2014-04-28-best-practices-arrays-in-firebase.html) – Kato

回答

2

假設你正在使用JavaScript SDK訪問火力地堡:

  1. ref.orderByChild('userId').equalTo('xxx')

  2. ref.orderByChild('itemOrdered').equalTo('def')

如果你想建立一個查詢,得到的順序項目def來自用戶xxx,那麼Firebase的查詢目前無法實現。查詢多個屬性的值的唯一方法是將它們組合到一個屬性中,以允許您想要的查詢。例如。

  • ref.orderByChild('userId_itemOrdered').equalTo('xxx_def')
  • +0

    Frank ..這正是我想要實現的..獲取用戶xxx訂購的所有項目。當你說這在firebase中是不可能的時......我不會理解你的聲明......你的解決方案.orderBY()。equalTo()在服務器端究竟做了什麼? – runtimeZero

    +0

    對不起,我誤讀了您的示例數據。在我的辯護中,項目'abc'和'def'有些難以解釋。我的意思是你只能查詢一個屬性。所以你可以得到所有物品的訂購,但不是所有用戶'xxx'訂購的'def'(除非你合併這些物業價值,就像我在#3中展示的那樣)。我會更新我的答案。 –

    0

    有根據您的平臺的附加選項。

    1) Query for the userId and then filter in code for the item you are looking for. 
    
    2) Query for the userId and build another query based on those results. 
    
    3) Flatten your data! 
    

    例如:創建一個名爲節點和user_purchases每個孩子可以與用戶的用戶ID節點購買的itemId的(這使得它能夠輕鬆地確切地知道用戶購買什麼物品)。或者創建一個items_purchased節點,每個子節點都是一個物品編號節點,然後是關聯的userId誰購買了該物品。