Вы можете использовать облачную функцию, чтобы сделать это. Давайте используем onCreate слушатель, чтобы усреднить общее количество каждый раз, когда есть запись.
export const ratingAvg = functions.database
.ref("ratings/{userId}/{recordId}")
.onCreate(async (snapshot, context) => {
const data = snapshot.val(); // rating obj (user, userPhoto, date, comment?, rating)
const userId = context.params.userId; // this is your userId
const transactionId = context.params.transactionId; // the uid of the record
const profileAvg = await admin
.database()
.ref(`ratings/${userId}/`)
.once("value");
//' if avgRating exists average with new rating else use new rating.
const newAvg = profileAvg.val().avgRating
? ((profileAvg.val().avgRating + data.rating) / 2).toFixed(2)
: data.rating;
// write new average
return admin
.database()
.ref(`ratings/${userId}/`)
.update({
avgRating: newAvg
})
.then(res => {
console.log(res);
})
.catch(err => {
console.log(err);
});
});