Question: I am working on a simple app for Android. I am having some trouble using the Firebase database since it uses JSON objects and I am used to relational databases. 

My data will consists of two users that share a value. In relational databases this would be represented in a table like this:

```
**uname1**  **uname2** shared_value

```

In which the usernames are the keys. If I wanted the all the values user Bob shares with other users, I could do a simple union statement that would return the rows where:

```
uname1 == Bob or unname == Bob

```

However, in JSON databases, there seems to be a tree-like hierarchy in the data, which is complicated since I would not be able to search for users at the top level. I am looking for help in how to do this or how to structure my database for best efficiency if my most common search will be one similar to the one above. 

In case this is not enough information, I will elaborate: My database would be structured like this: 

```
{
     'username': 'Bob' 
            {
                   'username2': 'Alice'
                         {
                               'shared_value' = 2
                         }
            }
      'username': 'Cece'
            {
                   'username2': 'Bob'
                         {
                               'shared_value' = 4
                         }
            }

```

As you can see from the example, Bob is included in two relationships, but looking into Bobs node doesn't show that information. (The relationship is commutative, so who is "first" cannot be predicted). 

The most intuitive way to fix this would be duplicate all data. For example, when we add Bob->Alice->2, also add Alice->Bob->2. In my experience with relational databases, duplication could be a big problem, which is why I haven't done this already. Also, duplication seems like an inefficient fix.

Answer: 
You can still have old structure:

```
[
    { username: 'Bob', username2: 'Alice', value: 2 },
    { username: 'Cece', username2: 'Bob', value: 4 },
]

```

You may want to create indexes on 'username' and 'username2' for performance. And then just do the same union.
To create a tree-like structure, the best way is to create an "ancestors" array that stores all the ancestors of a particular entry. That way you can query for either ancestors or descendants and all documents that are related to a particular value in the tree. Using your example, you would be able to search for all descendants of Bob's, or any of his ancestors (and related documents).

The answer above suggest:

```
{ "_id": {"uname1":"Bob", "uname2":"Alice"}, "value": 2 }

```

That is correct. But you don't get to see the relationship between Bob and Cece with this design. My suggestion, which is from Mongo, is to store ancestor keys in an ancestor array.

```
{ "_id": {"uname1":"Bob", "uname2":"Alice"}, "value": 2 , "ancestors": [{uname: "Cece"}]}

```

With this design you still get duplicates, which is something that you do not want. I would design it like this:

```
{"username": "Bob", "ancestors": [{"username": "Cece", "shared_value": 4}]}
{"username": "Alice", "ancestors": [{"username": "Bob", "shared_value": 2}, {"username": "Cece"}]}

```