简体   繁体   中英

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

We have a document where the key is 2 names like Bob_Jimmy

Every Name is Unique, but can appear in different setups like: 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? since we only know Bob

Create Firebase database reference and in onDataChange() method check wether the key contains your variable. 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. 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.

You can sort data using following methods

Method Usage

orderByChild() Order results by the value of a specified child key.

orderByKey() Order results by child keys.

orderByValue() Order results by child values

startAt() Return items greater than or equal to the specified key or value depending on the order-by method chosen.

endAt() Return items less than or equal to the specified key or value depending on the order-by method chosen.

equalTo() Return items equal to the specified key or value depending on the order-by method chosen.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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