How to extract data from a flattened structure

Here's my details:

{
    "users": {
        "someRandomUserId": {
            "name": "Name 1",
            "accounts": {
                "someRandomAccountId": true
            }
        },
        "someRandomUserId2": {
            "name": "Name 2",
            "accounts": {
                "someRandomAccountId": true,
                "someRandomAccountId2": true
            }
        }
    },
    "accounts": {
        "someRandomAccountId": {
            "name": "Account1",
            "users": {
                "someRandomUserId": true,
                "someRandomUserId2": true
            }
        },
        "someRandomAccountId2": {
            "name": "Account2",
            "users": {
                "someRandomUserId2": true
            }
        }
    }
}

      

After reading the docs, I believe this is how I should have set it up. If I am completely in the left margin, please let me know. My actual question is that I have the user id "someRandomUserId2", how do I get a list of the accounts the user is associated with along with their data? I would like to somehow return this data:

"accounts": {
    "someRandomAccountId": {
        "name": "Account1"
    },
    "someRandomAccountId2": {
        "name": "Account2"
    }
}

      

Or at least something like that. I tried to use startAt and endAt, but I can't figure out how to get them to work with the object:

accountRef
    .orderByChild('users.' + userId)        
    .equalTo(true)

      

UPDATE

Here is my current method, which seems like overkill:

var userRef = myDataRef.child('users');
var accountRef = myDataRef.child('accounts');

var accounts = {};

var userAccounts = userRef.child(userId + '/accounts');

userAccounts.on('child_added', function(dataSnap){  
    var accountId = dataSnap.key();

    var accountData = accountRef.orderByKey().equalTo(accountId);

    accountData.on('child_added', function(dataSnap){
        accounts[accountId] = dataSnap.val();
    });
    accountData.on('child_removed', function(){
        delete accounts[accountId];
    });
});
userAccounts.on('child_removed', function(dataSnap){
    var accountId = dataSnap.key();
    delete accounts[accountId];
});

      

+3


source to share


1 answer


You can add an additional structure to your database that specifically lists which users are part of the accounts, for example:

"accountusers": {
    "someRandomAccountId": {
        "someRandomUserId": true,
        "someRandomUserId2": true
    },
    "someRandomAccountId2": {
        "someRandomUserId2": true
    }
}

      

Then you can use the following query to get all the accounts owned someRandomUserId2

:



ref.child("accountusers").orderByChild("someRandomUserId2").equalTo(true)

      

This method requires everything to be accountusers

readable by the user.

orderByChild

and equalTo

only work at one level. Otherwise, it would be possible to write a similar query for your existing data structure.

+4


source







All Articles