1
{
 "users" : {
  "1320104182" : {
   "datas" : {
    "bio" : "some_data",
    "picture" : "some_data",
    "username" : "some_data",
    "website" : "some_data",
    "followers" : 14,
   }
  },
  "3271376571" : {
   "datas" : {
    "bio" : "some_data",
    "picture" : "some_data",
    "username" : "some_data",
    "website" : "some_data",
    "followers" : 10,
   }
  }
 }
}

I'm new to Firebase and i'm trying to do multiple think here without any success so far.

How can i retrieve a user by his "username" without knowing the key ?

Or how can i order the users by followers ?

I tried everything i could find in the documentation for a few hours i'm desperate.

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
Ethrak
  • 181
  • 2
  • 3
  • 12

2 Answers2

8

This seems fairly easy:

var ref = firebase.database().ref("users");
var query = ref.orderByChild("database/username").equalTo("some_data");
query.once("value", function(snapshot) {
  snapshot.forEach(function(child) {
    console.log(child.key, child.val().bio);
  });
});
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
0

Firebase is not so good when you need fancy queries. You must handle everything in your client (JavaScript) which is not the best approach when dealing with large data. In this case, I'd suggest you something like this:

const nameToSearch = 'John';
firebase.ref('users').once('value') //get all content from your node ref, it will return a promise
.then(snapshot => { // then get the snapshot which contains an array of objects
  snapshot.val().filter(user => user.name === nameToSearch) // use ES6 filter method to return your array containing the values that match with the condition
})

To order by followers, you can either also apply sort() (see example 1) or any of firebase default methods orderByChild() (see example 2), orderByKey (see example 3), or orderByValue (see example 4)

Example 1:

firebase.database().ref("users").once('value')
.then(snapshot => {
  const sortedUsers = snapshot.sort((userA, userB) => {
    if (userA.name < userB.name) {
      return -1;
    }
    if (userA.name > userB.name) {
      return 1;
    }
    return 0;
  })
})

Example 2:

var ref = firebase.database().ref("dinosaurs");
ref.orderByChild("height").on("child_added", function(snapshot) {
  console.log(snapshot.key + " was " + snapshot.val().height + " m tall");
});

Example 3:

var ref = firebase.database().ref("dinosaurs");
ref.orderByKey().on("child_added", function(snapshot) {
  console.log(snapshot.key);
});

Example 4:

var scoresRef = firebase.database().ref("scores");
scoresRef.orderByValue().limitToLast(3).on("value", function(snapshot) {
  snapshot.forEach(function(data) {
    console.log("The " + data.key + " score is " + data.val());
  });
});

Note: there might be typos in the examples, I wrote just to show you the idea of the concepts.

Check the following docs for more info:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

https://firebase.google.com/docs/reference/js/firebase.database.Query#orderByChild

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

Hope it helps

soutot
  • 3,531
  • 1
  • 18
  • 22
  • Thanks for this i understand it. So, Firebase is it relevant for medium size project if i have to fetch and sort a lot of queries client side ? – Ethrak Dec 20 '17 at 17:50
  • 1
    Personally, I had some performance issues while using firebase as database store, since it's not indexed you have some limitations on more complex queries (fancy where clauses, joins, pagination it's not that nice, etc), also it's data structure have some limitation, especially when you need data relation. After they release Cloud Functions it got a little better, since you can run some queries on the server side and request less memory from your client. But I still don't think it's a good idea to use it in larger apps. However, other features, like oauth, sms validation and push are great – soutot Dec 20 '17 at 19:21