![](/img/trans.png)
[英]How to get the auto-generated ID from .set() in firebase Firestore?
[英]Firebase - how to find all the match items with auto-generated ID, where values are false
遵循https://www.firebase.com/docs/web/guide/structuring-data.html上的技術
給定的URL根是: https : //myApp.firebaseio.com
數據是:
// Tracking two-way relationships between users and groups
{
"users": {
"mchen": {
"name": "Mary Chen",
"groups": {
"alpha": true,
"bob": false, // <- I want this
"charlie": true,
"dave": false // <- I want this
}
},
...
},
"groups": {
"alpha": {
"name": "Alpha Group",
"members": {
"mchen": true,
"hmadi": true
}
},
...
}
}
是否可以構造Firebase查詢來查找用戶“ mchen”(基本上我想要bob和dave)的所有值為“ false”的組? 怎么樣?
例如新的Firebase(' https://myApp.firebaseio.com/users/mchen/groups ')...
在我的真實代碼中,alpha,bob,charlie和dave是Firebase自動生成的鍵,但是相同的查詢應該可以解決我的問題。 (我正在使用Firebase Web版本)
提前致謝。
終於成功了。 我不是專家,但是我希望這對不熟悉Firebase的人有所幫助:
如果您僅使用Firebase:
var ref = new Firebase('https://myApp.firebaseio.com/users/mchen/groups');
ref.orderByValue().equalTo(false).on('value', function(data){
console.log(data.val());
})
如果您使用AngularFire:
var ref = new Firebase('https://myApp.firebaseio.com/users/mchen/groups');
$firebaseArray(ref.orderByValue().equalTo(false)).$loaded().then(function(data){
console.log(data);
});
注意:
性能還需要一個規則:
{
"rules": {
"users": {
"$userName": { // A better way would be using an auto-generated ID in here
"groups": {
".indexOn" : ".value" // add index on the value - notice it's .value
}
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.