1
votes

I have tried to make following and followers model like this. so every users will have following and followers subcollection

enter image description here

I only save minimum data in the document in following subcollection such as profile picture path, uid, and fullname

enter image description here

but the problem in this model is, when the user change their profile picture or their display name in 'users' parent collection, then the name and profile picture in the following subcollection of the follower is not updated.

so I think I need to use callable cloud function to solve this to update all documents in following subcollection

exports.callableUpdateUserDataInOtherDocuments = functions.https.onCall(async (data, context) => {



    try {

        const verifiedUserSnapshot = await db.doc(`users/${context.auth.uid}`).get()

        const userData = verifiedUserSnapshot.data()


        // update display name in following subcollection of the followers

        const changeUserDataInFollowersPromises = []
        const followersSnapshot = await db.collection(`users/${context.auth.uid}/followers`).get()

        followersSnapshot.forEach( doc => {

            const followerID = doc.data().uid
            const p = db.doc(`users/${followerID}/following/${userData.uid}`).update({
                fullname: userData.fullname,
                profilePicturePath: userData.profilePicturePath
            })

            changeUserDataInFollowersPromises.push(p)
        })

        return Promise.all(changeUserDataInFollowersPromises)

    } catch(error) {
        console.log(error)
        return null
    }





})

but using this cloud function update seems not efficient to me, if a user have 10.000 followers then I need to read 10.000 documents and update 10.000 documents. and also it seems the control is in the user because if they update the data 5 times in 1 minute then I need to read 50.000 documents and update 50.000 documents.

I need to also update the display name and profile picture path, because the documents in following subcollection will be displayed in recycler view (Android) and Table View (iOS)

I don't know, I am new in Firestore and in NoSQL database, it is a common way or is there a better way to solve this ?

1
There's always going to be tradeoffs between convenience, performance, and billing. I think you're going to have to figure out what you value most, and do what you need to do push those three in the direction that you prefer.Doug Stevenson

1 Answers

1
votes

Indeed, for your case, you will need to perform a lot of operations, in cases where a user has a lot of followers. You will need to balance the way that you are thinking your system, because Firestore and Cloud Functions will be able to handle and scale the performance, so you don't face any problems related to it.

However, the billing could be the main issue that you would be facing. For your use case, it's necessary that it will update the database this amount of time.

I did a search and I found the below links of discussions that might help you decide on your system and how it will work.

Let me know if this helped you!