2016-08-01 19 views
1

目前,Firebase不會按照輸入的順序存儲數據。對於前 - 可能有2個現有的孩子,並插入一個新的孩子,新的孩子去,並坐在已有的中間!我猜這是按字母順序工作的,但我想只按正確的順序存儲數據。Firebase:如何強制數據庫按照輸入的順序存儲值?

firebase.database().ref('someChildRef').set({ 
    //Could insert anywhere 
}); 

我覺得推動有可能以正確的順序插入,但推還產生我不想要一個獨特的鍵,我想插入的數據只能通過集並以正確的順序。

+0

不可能。使用查詢規則以您想要的順序恢復數據。 – theblindprophet

+0

@theblindprophet謝謝,'orderByChild()'爲我做了訣竅。 –

回答

0

不可能將數據插入到火力地堡到所需位置。 Firebase數據按字母順序排序,所有新插入的數據將相應地重新排列。

使用query規則可按所需順序返回數據。

如:

  • orderByChild()
  • orderByKey()
  • orderByValue()
  • equalTo()

你是正確的約push(),它總是會創建一個唯一的密鑰,並添加到末尾,就像推向數組w一樣虐待增加到最後。

0

這聽起來像你要找的火力地堡的push()方法,它基於時間戳的唯一關鍵:

firebase.database().ref('someChildRef').set({ 
    //Will result at "the end" 
}); 
0
var messageListRef = new Firebase('https://samplechat.firebaseio-demo.com/message_list'); 
var newMessageRef = messageListRef.push(); 
newMessageRef.set({ 'user_id': 'fred', 'text': 'Yabba Dabba Doo!' }); 
// We've appended a new message to the message_list location. 
var path = newMessageRef.toString(); 

嘗試使用push()方法,而不是一套。根據他們的文檔:

由push()生成的唯一名稱帶有客戶端生成的時間戳的前綴,以便生成的列表將按時間順序排序。

參考:https://www.firebase.com/docs/web/api/firebase/push.html

相關問題