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: 
Is there a reason why you don't invert this? How about a collection like:

```
{ "_id": 2, "usernames":[ "Bob", "Alice"]}
{ "_id": 4, "usernames":[ "Bob", "Cece"]}

```

If you need all the values for "Bob", then index on "usernames".

EDIT:

If you need the two usernames to be a unique key, then do something like this:

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

```

But this would still permit the creation of:

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

```

(This issue is also present in your as-is relational model, btw. How do you handle it there?)

In general, I think implementing an array by creating multiple columns with names like `"attr1"`, `"attr2"`, `"attr3"`, etc. and then having to search them all for a possible value is an artifact of relational table modeling, which does not support array values. If you are converting to a document-oriented storage, these really should be an embedded list of values, and you should use the document paradigm and model them as such, instead of just reimplementing your table rows as documents.
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.