2016-11-09 59 views
1

我有一個locations-orders表看起來像這樣:Firebase:如何爲嵌套字段添加索引?

{ 
    "0BW3H9T3R7HJB" : { 
    "orders" : { 
     "01750ea0-4980-4bc4-58b2-988c02324e671478636582396" : { 
     "created_at" : 1478636560000, 
     }, 
     "01750ea0-4980-4bc4-58b2-988c02324e671478636582483" : { 
     "created_at" : 1478636560000, 
     } 
    } 
} 

每個位置訂單節點有多個鍵/對象的orders節點。這些對象上有一個created_at字段。

我說這我的數據庫規則:

{ 
    "rules": { 
    ".read": true, 
    ".write": true, 
    "users": { 
     ".indexOn": "merchantId" 
    }, 
    "merchants": { 
     ".indexOn": "locations" 
    }, 
    "locations-orders": { 
     ".indexOn": ["orders/created_at"] 
    } 
    } 
} 

然而,火力地堡仍然抱怨說,我失去了一個索引:

Using an unspecified index. Consider adding ".indexOn": "created_at" at /locations-orders/1JS53G0TT5ZQD/orders to your security rules for better performance 

我應該運行的東西創建索引?還是它寫錯了?

=== UPDATE ===

我改變了我的文件來看看:

{ 
    "rules": { 
    ".read": true, 
    ".write": true, 
    "users": { 
     ".indexOn": "merchantId" 
    }, 
    "merchants": { 
     ".indexOn": "locations" 
    }, 
    "locations-orders": { 
     "$location_id": { 
     ".indexOn": ["orders/created_at", "orders/status_updated_at"] 
     } 
    } 
    } 
} 

,但我仍然得到同樣的警告:

Using an unspecified index. Consider adding ".indexOn": "created_at" at /locations-orders/1JS53G0TT5ZQD/orders to your security rules for better performance 
+0

您已經在您的問題中包含了JSON樹和規則的圖片。請將這些實際JSON和規則替換爲文本,您可以通過單擊Firebase數據庫控制檯中的導出按鈕輕鬆獲取這些文本。將JSON作爲文本可以搜索,使我們能夠輕鬆地使用它來測試您的實際數據並將其用於我們的答案中,而且一般而言,這只是一件好事。 –

+0

肯定會做! – Edmund

+0

@FrankvanPuffelen更新! – Edmund

回答

1

試試這個:

{ 
    "rules": { 
    ".read": true, 
    ".write": true, 
    "users": { 
     ".indexOn": "merchantId" 
    }, 
    "merchants": { 
     ".indexOn": "locations" 
    }, 
    "locations-orders": { 
     "orders": { 
     "$orderid": { 
      ".indexOn": "created_at" 
     } 
     } 
    } 
    } 
} 
3

如果你l在你的數據結構中,沒有orders/created_at/location-order/$orderId下。

{ 
    "rules": { 
    ".read": true, 
    ".write": true, 
    "users": { 
     ".indexOn": "merchantId" 
    }, 
    "merchants": { 
     ".indexOn": "locations" 
    }, 
    "locations-orders": { 
     "$someid": { 
     ".indexOn": ["orders/created_at"] 
     } 
    } 
    } 
} 
+0

對於'$ someid'寫什麼是否重要? – Edmund

+0

沒有。它就像你實際代碼中的一個變量名:只要你在規則內部使用相同的值(這裏不需要),實際的名字並不重要。 –

+0

我不必運行任何命令或任何東西?我更新它看起來像那樣,但它仍然顯示錯誤 – Edmund