我有一個content從 Firebase 實時數據庫中檢索到的節點。我利用它.orderByChild("time")來獲取按最早時間戳排序的前 5 個博客對象的數據快照。我我目前正在嘗試使用.indexOn : "time",這樣我就不必使用了,.orderByChild("time")因為我期望博客對象在檢索時已經按后端的時間戳排序。(我將要處理大量的博客對象,所以我想.indexOn : "time"在后端而不是orderByChild("time")在前端使用以提高效率)。目前,.indexOn不起作用,并且數據在檢索時未按其時間字段排序。查詢無.indexOn// this works fine// startAt is utilized for saving last blog object retrieved to retrieve more dataquery = FirebaseDatabase.getInstance().getReference() .child("content") .orderByChild("time") .startAt(nodeId) .limitToFirst(5);使用 .indexOn 查詢// this along with the Firebase rules below does not return the same result as above// startAt is utilized for saving last blog object retrieved to retrieve more dataquery = FirebaseDatabase.getInstance().getReference() .child("content") .startAt(nodeId) .limitToFirst(5);火力地堡規則:{ "rules": { ".read": true, ".write": true, "content" : { ".indexOn": "time" } }}Firebase 中數據的 JSON 結構:"content" : {"blog-0001" : { "content_id" : "blog-0001", "image" : "someimage", "time" : 13, "title" : "title1", "type" : "blog"},"blog-0002" : { "content_id" : "blog-0002", "image" : "someimage", "time" : 12, "title" : "title2", "type" : "blog"},"blog-0003" : { "content_id" : "blog-0003", "image" : "someimage", "time" : 11, "title" : "title3", "type" : "blog"},"blog-0004" : { "content_id" : "blog-0004", "image" : "someimage", "time" : 15, "title" : "title4", "type" : "blog"}...}
1 回答

HUWWW
TA貢獻1874條經驗 獲得超12個贊
您似乎誤解了什么.indexOn
,以及它與orderByChild("time")
.
要從content
ordered by(也可能是 filtered on)中檢索子節點time
,您將始終需要調用orderByChild("time")
.
如果您定義一個索引(使用".indexOn": "time"
),那么排序和過濾將在服務器上完成。
沒有索引,下的所有數據都content
將下載到客戶端,排序/過濾將在客戶端執行。
所以.indexOn
不能替代orderByChild
. 相反,兩者攜手合作以執行高效的數據排序和過濾。
添加回答
舉報
0/150
提交
取消