Firebase Query If Child of Child Contains a Value

Firebase query if child of child contains a value

Your current data structure is great to look up the participants of a specific chat. It is however not a very good structure for looking up the inverse: the chats that a user participates in.

A few problems here:

  • you're storing a set as an array
  • you can only index on fixed paths

Set vs array

A chat can have multiple participants, so you modelled this as an array. But this actually is not the ideal data structure. Likely each participant can only be in the chat once. But by using an array, I could have:

participants: ["puf", "puf"]

That is clearly not what you have in mind, but the data structure allows it. You can try to secure this in code and security rules, but it would be easier if you start with a data structure that implicitly matches your model better.

My rule of thumb: if you find yourself writing array.contains(), you should be using a set.

A set is a structure where each child can be present at most once, so it naturally protects against duplicates. In Firebase you'd model a set as:

participants: {
"puf": true
}

The true here is really just a dummy value: the important thing is that we've moved the name to the key. Now if I'd try to join this chat again, it would be a noop:

participants: {
"puf": true
}

And when you'd join:

participants: {
"john": true,
"puf": true
}

This is the most direct representation of your requirement: a collection that can only contain each participant once.

You can only index known properties

With the above structure, you could query for chats that you are in with:

ref.child("chats").orderByChild("participants/john").equalTo(true)

The problem is that this requires you to define an index on `participants/john":

{
"rules": {
"chats": {
"$chatid": {
"participants": {
".indexOn": ["john", "puf"]
}
}
}
}
}

This will work and perform great. But now each time someone new joins the chat app, you'll need to add another index. That's clearly not a scaleable model. We'll need to change our data structure to allow the query you want.

Invert the index - pull categories up, flattening the tree

Second rule of thumb: model your data to reflect what you show in your app.

Since you are looking to show a list of chat rooms for a user, store the chat rooms for each user:

userChatrooms: {
john: {
chatRoom1: true,
chatRoom2: true
},
puf: {
chatRoom1: true,
chatRoom3: true
}
}

Now you can simply determine your list of chat rooms with:

ref.child("userChatrooms").child("john")

And then loop over the keys to get each room.

You'll like have two relevant lists in your app:

  • the list of chat rooms for a specific user
  • the list of participants in a specific chat room

In that case you'll also have both lists in the database.

chatroomUsers
chatroom1
user1: true
user2: true
chatroom2
user1: true
user3: true
userChatrooms
user1:
chatroom1: true
chatroom2: true
user2:
chatroom1: true
user2:
chatroom2: true

I've pulled both lists to the top-level of the tree, since Firebase recommends against nesting data.

Having both lists is completely normal in NoSQL solutions. In the example above we'd refer to userChatrooms as the inverted index of chatroomsUsers.

Cloud Firestore

This is one of the cases where Cloud Firestore has better support for this type of query. Its array-contains operator allows filter documents that have a certain value in an array, while arrayRemove allows you to treat an array as a set. For more on this, see Better Arrays in Cloud Firestore.

Firebase - Query Child of Child

Firebase Realtime Database queries work on a flat list of nodes. The key/value you are ordering/filtering on must be at a fixed path directly under each direct child node of the location you search.

The simplest solution here seems to be to add an additional data structure that maps the inviteCode values back to their category (and possibly other data about them). This is sometime referred to as a inverted index, and is quite common in NoSQL databases.

So you'd have:

invoteCodesLookup: {
"271429": {
category: "Maths",
...
}
}

You can keep this and the original data structure in sync with multi-path updates and security rules.

For more on this, see:

  • Firebase Query Double Nested
  • Firebase query if child of child contains a value
  • Many to Many relationship in Firebase
  • Firebase simple many to many relationship
  • Many to many relationships on Firebase
  • How do I load/retrieve a Many to Many relationship in Firebase Realtime Database

How to get child of child value from firebase whose parent value is unknown in java

After reading the various answers on this topic linked above by Mr. @Frank van Puffelen and spending some times over it, the problem is finally solved now without changing my database structure.
Below is screenshot of the result which I wanted:

Sample Image

Here is my modified and working code :

private void retrieveData() {

final String shift = kvName.getText().toString();
final String employeeCode = empCode.getText().toString();

final DatabaseReference aparRef = dbRef.child("Apar").child(shift);

aparRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

list = new ArrayList<>();

if (dataSnapshot.exists()) {

for (DataSnapshot dataS1 : dataSnapshot.getChildren()) {

if (dataS1.hasChild(employeeCode)) {

AparData aparData = dataS1.child(employeeCode).getValue(AparData.class);
list.add(aparData);

rvAPAR.setHasFixedSize(true);
rvAPAR.setLayoutManager(new LinearLayoutManager(Apar.this));
rvAPAR.setItemAnimator(new DefaultItemAnimator());
aparAdapter = new AparAdapter(Apar.this, list);
rvAPAR.setAdapter(aparAdapter);

} else {
Toast.makeText(Apar.this, "No Data Found!!", Toast.LENGTH_SHORT).show();
}

}
} else {
Toast.makeText(Apar.this, "Not Data Found!!", Toast.LENGTH_SHORT).show();
}
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}
});

}

Check for a value in many child nodes in firebase real-time database

A Firebase query operates on a flat list of nodes, and can only contain a single unknown key.

From what I can tell you have at least two levels of unknown keys (-MH...Ez12, and 9alMk....QS2) and possibly three (Private Group). There is no way to query this structure with a database query, and you will need to come up with a different data structure to allow your use-case.

For example, if you want to allow finding the posts for a given contact ID, consider storing exactly that relationship:

"contactsToPosts": {
"$contactId": {
"$postId": true
}
}

For more on this sort data modeling trade-off, see:

  • Firebase Query Double Nested
  • Firebase query if child of child contains a value

Firebase search by child value

You can use equalTo() to find any child by value. In your case by name:

ref.child('users').orderByChild('name').equalTo('John Doe').on("value", function(snapshot) {
console.log(snapshot.val());
snapshot.forEach(function(data) {
console.log(data.key);
});
});

The purpose of orderByChild() is to define the field you want to filter/search for. equalTo() can get an string, int and boolean value.

Also can be used with auto generated keys (pushKey) too.

You can find all the documentation here

Query firebase realtime database where child has property

Your current data structure makes it easy to find all the users for a specific game. It does not however make it easy to find all the games for a specific user. To allow that, you'll want to add an addition data structure that inverts the information.

So that'd look something like this:

player_games: {
"XDYNyN8il6TDsM4LuttwDzNuytj1": {
"-M5vf...U5zK": true
},
"NxH14...mxY2": {
"-M5vf...U5zK": true
}
}

Also see:

  • Firebase query if child of child contains a value
  • Firebase Query Double Nested

I recommend you also study the Firebase documentation on structuring your database, specifically the section on avoiding nested data. By mixing entity types as you currently do, you'll likely run into problems with security, and scalability.

The most idiomatic way to model your many-to-many relationship in the Firebase database is with four top-level lists:

players: {
$playerId: { ... }
}
games: {
$gameId: { ... }
}
player_games: {
$playerId: {
$gameId: true
}
}
game_players: {
$gameId: {
$playerId: true
}
}

Also see:

  • Many to Many relationship in Firebase

Firebase retrieve a document based on grandchild's value which is a collection

There is no way in a Firebase Realtime Database query to check for the existence of a value in an array. If you're trying to do this, it typically means that your data structure is actually not an array, but a set.

Since a set data type doesn't exist, you can't store it in Firebase natively though. The closest equivalent is not an array though, but a map like this:

"barcodes": {
"12345": true,
"5678": true
}

This may look a bit weird at first, but it has the exact properties that you're typically looking for in a set: the values (that are now keys) are by definition unique in the parent node, and you can test for the presence of a specific value/key.

Unfortunately, you still won't be able to query on this structure, as you can only define indexes on keys that you know, and I'm assuming that the barcodes are a rather infinite set of values.

So instead you'll have to define an inverted data structure, as I've explained here: Firebase query if child of child contains a value



Related Topics



Leave a reply



Submit