Firebase database query in "relation" sorted

1

Given the documentation I got 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

  • Is the squeeze adequate? From what I read this is the right way to denormalize.
  • You can list all missions of a user ordered by value ? (hence the question if the structure is correct)
  • With the code below I can list all missions of a user, but I could not sort 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);
    });
    
        
    asked by anonymous 29.12.2016 / 13:38

    1 answer

    0

    Try the code below:

    var missions = firebase.database().ref('users/'+uid+'/missions').orderByChild('value');
    
        
    23.01.2017 / 00:17