How would you structure mutual rating social network model on Firestore? [closed]

左心房为你撑大大i 提交于 2020-03-06 09:44:08

问题


I think it would be easier to display my model with an example. The functionality of the app would be the following: It would be social network schema. A mutual friend model. When a user logins in the app, a list will be displayed. Each list item consist of a friend name - photos(photo1, photo2) - rating for each photo(calculated based on both ratings).

Example:
Logged in as UserA:

list item1:
[
 name: UserBName,
 photo1: 5 (UserA rated it 2, UserB rated it 8),
 photo2: 4 (UserA rated it 3, UserB rated it 5)
],
list item2:
[
 name: UserCName, 
 photo1: 2 (UserA rated it 1, UserC rated it 4)
]

I was thinking of creating a friend subcollection for each user document and inside having a structure like that:

UserB: {
     name: UserBName,
     photo1:{
      rating from UserA: 4
      rating from UserB: 2
     },
     photo2:{
      rating from UserA: 3
      rating from UserB: 5
     }
    }

My issue is that UserB(document)->friends(subcollection)->UserA(document) would have the same data and i am not so sure if its a viable solution.


回答1:


Duplicating data and denormalizing data is common practice in NoSQL databases. It's used to flatten structures so queries can be run against the data. In the question we don't know what kind of queries you want to run other than

When a user logins in the app, a list will be displayed. Each list item consist of a friend name - photos(photo1, photo2) - rating for each photo(calculated based on both ratings).

Which doesn't require a query. Using the structure in the question, when UserB logs in, your app will read the UserB document which will contain their name and all of their photo's and ratings so all of that data will be available as soon as the document is read.

However...

If each user has LOTS of photos's that could be a LOT of data to be read in and could overwhelm the device. Additionally, if you are really trying to avoid duplicating data, then you need to put the data in one place.

I would change the structure completely to provide more flexibility in only reading in the data you want.

First a simpler user collection: the documentId's are the users uid and then store their name etc as field within each document

users
   uid_0:
      name:
   uid_1:
      name:

Then a photos collection that stores the ratings

firestore_root
   photos
      photo_0
         uid_0: 4
         uid_1: 2
      photo_1
         uid_3: 2
         uid_2: 5
      photo_2
         uid_0: 3
         uid_1: 1

So then when a user logs in, perform a query on the photo's node for all photos that contain a users uid with a rating > -1 (assuming ratings go from 0 to 5 for example). In this case that would be photo_0 and photo_2

in pseudo-code, to get all of the nodes that uid_1 has rated

queryPhotos() {
    photoCollection.whereField("uid_1", isGreaterThan: -1).getDocuments() { querySnapshot, error in
        //the snapshot will contain photo_0 and photo_2
        for doc in querySnapshot.documents { //iterate over each photo
            //you'll have all of the fields here and can calculate an average rating
            print(doc.documentID, doc.data())
        }
    }
}



回答2:


why not just :

list item :[

userB :{...}

userC : {...}

]

userB (and userC) will use the structure you post below, photo1 and photo2 for each item seems redundant



来源:https://stackoverflow.com/questions/60260094/how-would-you-structure-mutual-rating-social-network-model-on-firestore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!