简体   繁体   中英

firestore data model using multiple sub collections

I have a current structure in my firestore.

Data model:

- users (collection)
    - 9d5UCo4RtyTosiFAIvnPK4zFCUk1 ( each user is a separate document )
        - trips ( collection )
            - New York (Document)
               - fields: date, description, photos:[url1, url2, ....] ( fields in the New York document), status:public
            - Los Angeles
               ....

Now I want to add notes, likes and metadata for each photo in the New York, LA, etc photos array. I have a few ideas but not sure which is scalable and works well for what I want to do with notes, likes and metadata.

The notes don't have to load at the same time the photos do. The user would click to open notes which could be a new query to DB. But each photo does need to know how many notes are associated with each photo when photos are been displayed.

Solution 1:

photos array would be an object instead.

const photos = {
  isdffd: {
    url: "path",
    likes: 100,
    metadata: {
       ... 
    },
  },
  xxydfsd: {
    url: `path`,
    likes: 10,
    metadata: {
        ...
    }
  },
};

Then I have a collection of notes back up where trips is and use the photo id as the id for the note. but that limits me to 1 note per photo which I want to be able to add multiple notes.

I also want to be able to build a search page which would search through all notes throughout all trips and display them grouped by trip in the results.

What would be a good approach for this kind of setup with what I have right now which is using sub-collections for most of my structure so far.

Solution 2:

add another sub-collection inside each trip document called photos each photo would be its own document with likes, metadata and notes. But then notes needs to be a sub-collection of each photo document. This would make it hard to search all trips and all notes for each photo.

The way your original function has been structured:

- users (collection)
    - 9d5UCo4RtyTosiFAIvnPK4zFCUk1 ( each user is a separate document )
        - trips ( collection )
            - New York (Document)
               - fields: date, description, photos:[url1, url2, ....] ( fields in the New York document), status:public
            - Los Angeles
               ....

If you implement the first solution, you will limit your code if in the future you want to add another field to the photos object, or as you add additional photos to the object .

Adding a photos sub-collection inside your trips is your best option, this way your collection can grow and avoid the limitation of the first solution (having to make any change manually). If your notes are only text, then you can keep them inside each photo document as an array, avoiding another subcollection. Keeping the photos as a sub-collection and keeping the URL, likes, metadata and notes (if they are only text) inside each photo created should not add much difficulty when querying.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM