-1

I have read about this a lot, but never in C#:

I am trying to access a user and his profile picture:

tblUsers -> 1 (id) -> name: test, age : 12

tblUserPic -> 1 (id) ->picture : long pic, belongsTouserID: 1

So every user has a profile picture and I wanna "join" those tables together and get the user and his profile picture.

I was able to get either:

public static async Task<Dictionary<string, object>> GetElementById(FirestoreDb db, Documents collection, string documentId)
{
    DocumentReference docRef = db.Collection(collection.ToString()).Document(documentId);
    DocumentSnapshot snapshot = await docRef.GetSnapshotAsync();
    if (snapshot.Exists)
    {
        return snapshot.ToDictionary();
    }
    else
    {
        return null; ; 
    }
}

But never both.

How can I "join"? Or what is the fastest way of getting my data?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
innom
  • 770
  • 4
  • 19

1 Answers1

2

Queries in Firestore are shallow, meaning that it can only return documents from the collection that the query is run against. If you need to get documents from two collections, you'll have to perform two queries and combine the result on the client. There is no way you can perform an SQL JOIN.

But in your particular case, I would store the profile picture inside the User documents. In this way, you only need to perform a single query.

What you just did, is called denormalization and it's common practice when it comes to NoSQL databases. But in this case, there is no need for that.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Hey innom. Can I help you with other information? – Alex Mamo Aug 26 '22 at 08:27
  • Ok thank you. I will thent try to create multiple tables for the user, depeninding on the severity of data securtiy concerrs. eg: one table for email and password, one for profile pictures (thumbnail and full), one for personal data and one for less important data (last login, etc). Then do the queries in regards to my needs. thank you! – innom Aug 29 '22 at 09:35