DEV Community

Niko Montana
Niko Montana

Posted on

1

Firestore Modelling Data

Hi,

I want to create an instagram clone with firebase and found some limitations while working on the datastructure.

As the documentation reccomends to keep the documents as small as possible and the collections big I have created this model.

+ Profile (collection)

  |--- uid (document)

      |--- userName (String)

      |--- firstName (String)

      |--- lastName (String)

      |--- profileImgUrl (String)

      |--- userName (String)

      |--- UserPosts (collection)

         |--- uid (document)

            |--- title (String)

            |--- createdAt (timeStamp)

            |--- description (String)

            |--- imgUrl (String)

I have decided to go with this model because by splitting Profile and Post into seperate root collections I am not able to query Posts by userName without a second query after I get the uid of the user.

My problem now is that I dont know how to write a query to get the posts of a user by userName

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (1)

Collapse
 
razbakov profile image
Aleksey Razbakov

I would recommend to have 2 collections in root: Profiles and Posts. Each Post should have userId and userName (or any other field you want to be able to filter).

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay