Firebase database query in ordered "relation"

Asked

Viewed 341 times

2

Given the documentation I reached the following bank structure

    {   
    "users":{
        "5511995278831": {
            "name": "User 1",
            "email": "[email protected]",
            "about": "sdf sdf sdf",
            "sms_code": "10",
            "active": "1",
            "os": "android",
            "push_token": "",
            "missions": {
                "-K_0rqR_eUzr0S9LSRk5": true,
                "-K_0sJH6ZXDUtf62W_LF": true
            }
        },
        "5511941264978": {
            "name": "User 2",
            "email": "[email protected]",
            "about": "9sfs9d 8sf9sfsknfd",
            "sms_code": "99",
            "active": "1",
            "os": "ios",
            "push_token": "",
            "missions": {
                "-K_0sJH6ZXDUtf62W_LF": true
            }
        }
    },

    "missions": {
        "-K_0rqR_eUzr0S9LSRk5": {
            "name": "Mission 1",
            "value": 300,
            "end_date": "897289732948",
            "description": "kfj lsdfkjlsdkjfs",
            "cover": null,
            "is_closed": false,
            "created_at": "93209482384902",
            "users": {
                "5511941264978": true,
                "5511995278831": true
            }
        },
        "-K_0sJH6ZXDUtf62W_LF": {           
            "name": "Mission 2",
            "value": 100,
            "end_date": "124234238764",
            "description": "kfj lsdfkjlsdkjfs",
            "cover": null,
            "is_closed": false,
            "created_at": "347298479234",
            "users": {
                "5511941264978": true
            }
        }
    }
}

Now I have some doubts

  1. Is this suitable? From what I read this is the right way to denormalize.
  2. It is possible to list all missions of a user ordered by value? (gives the doubt if the structure is correct)

With the code below I can list all missions of a user, however I could not order them by value.

var uid = '5511941264978';
var missions = firebase.database().ref('users/'+uid+'/missions');
missions.on("child_added", function(mission, parent){
    console.log('mission');
    console.log(mission.key, mission.val());
    firebase.database().ref('missions/'+mission.key).on('value', function(mis){
        console.log(mis.val());
    });
}, function(err){
    console.log(err);
});

1 answer

0

  1. The structure is suitable?

    For what you intend to do, I believe you are well suited.

  2. You can list all a user’s Missions sorted by value?

    Yes. Try the code below:

    var missions = firebase.database().ref('users/'+uid+'/missions').orderByChild('value');
  • Please add a brief explanation about your code. Can answer both OP questions?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.