Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase, query "not equal to"

Here my database structure :

{
  "feeling" : {
    "fake0" : {
      "fake1" : {...},
      "fake2" : {...}
    }
  },
  "profile" : {
    "fake0" : {...},
    "fake1" : {...},
    "fake2" : {...},
    "fake3" : {...}
  }
}

If the logged user is "fake0", how can he fetch all profiles that are not in his feeling ?

I would like to do something like : ref("/profile", {notEqualTo: ["fake1","fake2"]}).on('once', .....)

Perhaps my structure is bad, just tell me a better one and I will change it :) !

like image 669
Charly berthet Avatar asked Aug 17 '17 15:08

Charly berthet


People also ask

Can We order data by value in Firebase?

We can also order data by value. Let us add the ratings collection in Firebase. Now we can order data by value for each player. Let us consider the following example. The output will be as shown below.

Does Cloud Firestore support not- equal queries?

Hi, Firebase developers! In what might be my shortest (but most exciting) blog post this year, we wanted to let you know that Cloud Firestore now has support for not-equal queries. This means you can now query, for example, all documents in a "Projects" collection where the project's status field is not equal to the value "completed"

What are not-in and == Queries in Firebase?

The Firebase release notes suggest there are now not-in and != queries. ( Proper documentation is now available.) not-in finds documents where a specified field’s value is not in a specified array.

What happens when you filter a firebase object?

When we perform the filter operation, the whole firebase object was added back to the array instead of just the data. We can solve this by chaining the filter method with the map method. FYI: This is an expensive operation because you will fetching all the articles from database and then filtering them locallly.


1 Answers

This and a few other similar questions (Firebase: Query to exclude data based on a condition) and my experience with Firebase leads me to believe that there aren't any functionalities in Firebase that do this directly.

Now you can filter by property, which isn't entirely what you're looking for, but it might be useful for you to structure your database so that user profiles also contain reference to the /users who contain them/ in their feeling. Thus, your query would get much easier. You probably also want to, as mentioned in the other StackOverflow thread linked above, use the user ID as the key and store its value as "true." This way you can check by ID, which if I recall correctly is easier than checking value.

I know Firebase also recommends that you store a little bit of redundant information; it's a matter of whether you want to put in the effort when writing or reading.

like image 81
Giselle Serate Avatar answered Sep 23 '22 06:09

Giselle Serate