简体   繁体   English

Firebase - 从密钥包含VAR的子项中检索所有数据

[英]Firebase - Retrieve all data from child where key contains VAR

Working with Android and FireBase, and stumpled upon a problem, from what we wanna achieve 使用Android和FireBase,并根据我们想要实现的问题解决问题

We have a document where the key is 2 names like Bob_Jimmy 我们有一个文件,其中键是Bob_Jimmy这两个名字

Every Name is Unique, but can appear in different setups like: Bob_Jimmy Jimmy_Bob Bob_Carl 每个名称都是唯一的,但可以出现在不同的设置中,如:Bob_Jimmy Jimmy_Bob Bob_Carl

Since this is the key, how should/would you go about getting data from all the keys where 'Bob' is part of the key? 由于这是关键,你应该如何/你将如何从'Bob'作为密钥一部分的所有密钥中获取数据? since we only know Bob 因为我们只认识鲍勃

Create Firebase database reference and in onDataChange() method check wether the key contains your variable. 创建Firebase数据库引用并在onDataChange()方法中检查密钥是否包含您的变量。 For example: 例如:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
ref.addListenerForSingleValueEvent(new ValueEventListener(){

  @Override
  public void onDataChange(DataSnapshot dataSnapshot){
     if(dataSnapshot.getKey().contains("bob")){
             {your code}
       }
  }

 });

if I have your youe database structure i may be able to write accurate code. 如果我有你的数据库结构,我可能能够编写准确的代码。 This is just a sample. 这只是一个样本。 You can use addChildEventListener() if u want to listen to new child adding events. 如果要监听新的子级添加事件,可以使用addChildEventListener()。 Hope it helps. 希望能帮助到你。

You can use the Realtime Database Query class to retrieve data sorted by key, by value, or by value of a child. 您可以使用实时数据库查询类来检索按键,值或子值的值排序的数据。 You can also filter the sorted result to a specific number of results or a range of keys or values 您还可以将排序结果过滤为特定数量的结果或一系列键或值

Note: Filtering and sorting can be expensive, especially when done on the client. 注意:过滤和排序可能很昂贵,尤其是在客户端上完成时。 If your app uses queries, define the . 如果您的应用使用查询,请定义。 indexOn rule to index those keys on the server and improve query performance as described in Indexing Your Data. indexOn规则,用于索引服务器上的这些键并提高查询性能,如索引数据中所述。

You can sort data using following methods 您可以使用以下方法对数据排序

Method Usage 方法用法

orderByChild() Order results by the value of a specified child key. orderByChild()按指定子键的值对结果进行排序

orderByKey() Order results by child keys. orderByKey()按子键排序结果。

orderByValue() Order results by child values orderByValue()按子值排序结果

startAt() Return items greater than or equal to the specified key or value depending on the order-by method chosen. startAt()返回大于或等于指定键或值的项,具体取决于所选的order-by方法。

endAt() Return items less than or equal to the specified key or value depending on the order-by method chosen. endAt()返回小于或等于指定键或值的项目,具体取决于所选的order-by方法。

equalTo() Return items equal to the specified key or value depending on the order-by method chosen. equalTo()返回等于指定键或值的项目,具体取决于所选的order-by方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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