2017-09-25 64 views
1

我在火力地堡許多電報鍵盤,我想有一個高級Dictionery搜索的Keyboards童車,我的意思是SECEND級深,即用戶輸入,
Forexample用戶輸入rock ,我想每個鍵盤包含在岩石開始在中間或者,在這個例子中我有3個鍵盤:morningrockrocky火力地堡高級詞典搜索

const ref = db.ref('Keyboards/rock'); //keyboard 1 
const ref = db.ref('Keyboards/morning'); //keyboard 2 
const ref = db.ref('Keyboards/rocky'); //keyboard 3 

?我怎樣才能和console.logrockrocky當用戶鍵入rock
我的問題90%由@rymdmaskin解決,但高級字典搜索仍然是開放的。
equalTostartAtendAt不行的,我需要像contain,有人說用彈性搜索(https://firebase.googleblog.com/2014/01/queries-part-2-advanced-searches-with.html

結構: enter image description here

規則:

{ 
    "rules": { 
    ".read": true, 
    ".write": true, 
     "Keyboards" : { 
      ".indexOn": "Keyboards" 
     } 
    } 
} 

代碼:

const ref = db.ref('/'); 
    ref.child('/').orderByChild('Keyboards').equalTo('rock').on("value", function(snapshot) { 
    console.log(snapshot.val()); 
    key = snapshot.forEach(function(data) { 
     console.log(data.key); 
    }); 
}); 
+0

你是什麼意思是把那些在一個字符串?你想要達到什麼目標?你的意思是在某種全局變量中連接每個子節點的內容嗎? – adz5A

+0

例如我有一個書名的銀行,當用戶在bot中輸入「rock」時,bot會發送包括'rock'名的所有書籍,在這個例子中我有搖滾和搖滾樂,bot應該向用戶發送2條消息,岩石和岩石有一些chids:id,文本,照片和..... –

+1

Firebase數據庫查詢無法深入地搜索嵌套的屬性。看到我的解釋在這裏:https://stackoverflow.com/questions/27207059/firebase-query-double-nested我也懷疑你正在處理一個分類問題,你的數據結構是不可用的。看到我的解釋在這裏:http://stackoverflow.com/questions/40656589/firebase-query-if-child-of-child-contains-a-value –

回答

2

如果我找到了你,你想要做這樣的事情。

結構:

- Keyboards 
    - 0 
    - name: "rocks" 
    - children 
     - 0: "id" 
     - 1: "desc" 
    - 1 
    - name: "rocky" 
    - children 
     - 0: "id" 
     - 1: "desc" 

規則:

{ 
    "rules": { 
    ".read": true, 
    ".write": true, 
     "Keyboards" : { 
     "$key": { 
      ".indexOn": "name" 
     } 
     } 
    } 
} 

代碼:

const ref = db.ref('/'); 
    ref.child('Keyboards').orderByChild('name').equalTo('rocks').on("value", function(snapshot) { 
    console.log(snapshot.val()); 
    key = snapshot.forEach(function(data) { 
     console.log(data.key); 
    }); 
}); 

這是你在找什麼?否則,請提供更詳細的信息。目前還不清楚你想達到什麼目的。

+0

是你的結構相同,我的結構? –

+0

我已將'childs'重命名爲'children' – Orlandster

+0

我在我的終端中收到null –

1

我的建議是要麼更改數據庫使用此查詢這似乎已經解決了這個問題的結構:

.orderByKey().startAt("rock") 

我不敢肯定它會爲你的將來所有的情況下,壽工作因爲我認爲查詢將開始於搖滾但不結束在搖滾如果你有更多的鍵盤後搖滾。我只是這個基礎源在此:Common SQL Queries converted for the Firebase Database

enter image description here

+0

我有另一個功能,讓搖滾樂和岩石的孩子,經過轉換,發送數據給用戶。 –

+0

我的問題解決了95%的代碼,有沒有這樣的事情:'.orderByKey()。contains(「rock」)'? ,併爲了字典搜索我應該如何實施? –