Imagine having two main collections: one for users and another for stories. Whenever a user document is updated (specifically the values username
or photoUrl
), you want to mirror those changes on corresponding documents in the story collection.
A sample user document might resemble this (simplified):
{
username: 'blubber',
photoUrl: 'my/photo/path',
uid: 'usersUniqueId'
}
This would be an example of a story document (simplified):
{
username: 'blubber',
photoUrl: 'my/photo/path',
uid: 'usersUniqueId',
sid: 'storiesUniqueId,
content: '...'
}
When it comes to cloud functions, you're unsure how to retrieve all story documents that match the user's id. The current code snippet looks like this:
export const onUpdateUser = functions.firestore.document('/users/{userId}')
.onUpdate((change, context) => {
const before = change.before.data();
const after = change.after.data();
if(before.username !== after.username || before.photoURL !== after.photoURL) {
return admin.firestore().collection('/stories/')
.where(before.uid, '==', ***fetch the comparison***)
// perform specific action
} else {
return null;
}
});
Any assistance regarding this matter would be greatly appreciated.