简体   繁体   English

Firebase-如何查找所有具有自动生成的ID(值为false)的匹配项

[英]Firebase - how to find all the match items with auto-generated ID, where values are false

Following the techniques on https://www.firebase.com/docs/web/guide/structuring-data.html 遵循https://www.firebase.com/docs/web/guide/structuring-data.html上的技术

Given the url root is: https://myApp.firebaseio.com 给定的URL根是: https : //myApp.firebaseio.com

And the data is: 数据是:

  // 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
        }
      },
      ...
    }
  }

Is it possible to construct a Firebase query to find all the groups that have the value 'false' for user "mchen" (basically I want bob and dave)? 是否可以构造Firebase查询来查找用户“ mchen”(基本上我想要bob和dave)的所有值为“ false”的组? How? 怎么样?

eg new Firebase(' https://myApp.firebaseio.com/users/mchen/groups ') ... 例如新的Firebase(' https://myApp.firebaseio.com/users/mchen/groups ')...

In my real code, alpha, bob, charlie and dave are Firebase auto-generated keys, but the same query should solve my problem. 在我的真实代码中,alpha,bob,charlie和dave是Firebase自动生成的键,但是相同的查询应该可以解决我的问题。 (I'm using Firebase Web version) (我正在使用Firebase Web版本)

Thanks in advance. 提前致谢。

Finally got it working. 终于成功了。 I'm not an expert, but I hope this helps anybody not familiar with Firebase: 我不是专家,但是我希望这对不熟悉Firebase的人有所帮助:

If you only use 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());
})

If you use AngularFire: 如果您使用AngularFire:

var ref = new Firebase('https://myApp.firebaseio.com/users/mchen/groups');
$firebaseArray(ref.orderByValue().equalTo(false)).$loaded().then(function(data){
  console.log(data);
});

Note: 注意:

  • orderByValue() is the one to use without knowing the keys orderByValue()是在不知道键的情况下使用的一种
  • Use equalTo(false), not equalTo('false') 使用equalTo(false),而不是equalTo('false')
  • $firebaseArray is the one that returns multi items, not $firebaseObject $ firebaseArray是返回多个项目的对象,而不是$ firebaseObject

A rule is also required for performance: 性能还需要一个规则:

{
  "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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM