如何创建“喜欢计数”; Cloud Firestore聚合功能? [英] How to create "likes count" Cloud Firestore aggregate function?

查看:75
本文介绍了如何创建“喜欢计数”; Cloud Firestore聚合功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是firebase的新手,我想为喜欢计数创建一个聚合函数。



我有三个根集合:供稿,喜欢和用户。 / p>

提要具有以下字段:





说明:<饲料说明>



likeCount:< like的总数>



title: <提要标题>



userId:<提要的用户ID>






喜欢的字段如下:





feedId:<用户给出喜欢的Feed的ID& gt;



likeBy:<喜欢该供稿的用户的ID>



likeTo:<用户的ID(如何创建供稿)>






用户具有以下字段:



用户名:<用户名>



电子邮件:<用户电子邮件>



当用户喜欢Feed时,会在喜欢集合中添加新条目。



我正在尝试创建一个当用户在订阅源上提供赞时,聚合函数将订阅源集合中的likeCount增加






我正在寻找解决方案。我找到了以下嵌套结构的解决方案








因此,我的问题是是否有可能通过我的数据结构为喜欢计数创建一个聚合函数(三个根集合:提要,喜欢和用户)?如果是,那么如何实现?



还是我需要更改数据结构?

解决方案

我已经实现了一个类似的功能,我使用了 Firebase云功能



因此您可以执行以下操作:



只要添加了一些内容在特定位置的数据库中,将'likesCounter'加1:

  exports.countLikes = functions.firestore 
.document('likes')。onWrite((change,context)=> {
const data = change.after.val();
const count = Object.keys(data).length;
返回change.after.document.collection('likesCount')。set(count-1);
});

^^您可以将该代码放入Firebase函数项目文件夹的index.js文件中。 / p>

您需要更改上面的代码以适合数据库的数据结构。请遵循这些步骤开始使用Firebase项目上的云功能。不熟悉。


I am newb to firebase, I want to create an aggregate function for the likes count.

I have three root collection: feeds, likes, and users.

feeds have the following fields:

description: <description of feed>

likeCount:<Total count of like>

title: <feed title>

userId: <userId of feed>


likes have the following fields:

feedId: <id of the feed where a user gives like>

likeBy: <id of the user who likes the feed>

likeTo: <id of the user how created a feed>


users have the following fields:

username: <User name>

email: <User email>

When a user likes a feed then new entry added in likes collection.

I am trying to create an aggregate function to increase likeCount in feed collection when the user gives like on a feed.


I am checking for a solution. I have found a solution for the nested structure like following


So, my question is that is it possible to create an aggregate function for the likes count with my data structure (three root collection: feeds, likes, and users.)? If yes then How can I achieve it?

or Do I need to change my data structure?

解决方案

I have implemented a similar feature in mine, I used Firebase Cloud Functions

So you could do something like this:

Whenever something get's added to the database at a particular location, increment the 'likesCounter' by one:

exports.countLikes = functions.firestore
.document('likes').onWrite((change, context) => {
 const data = change.after.val();
 const count = Object.keys(data).length;
 return change.after.document.collection('likesCount').set(count - 1);
});

^^ You would put that code in the index.js file of your firebase functions project folder.

You'll need to change the code above to fit the data structure of your database. Follow theses steps to get started with cloud functions on your firebase project if you're not familiar.

这篇关于如何创建“喜欢计数”; Cloud Firestore聚合功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆