【问题标题】:How to select an array into document firestore?如何将数组选择到文档 Firestore 中?
【发布时间】:2021-03-11 12:29:14
【问题描述】:

这似乎是我要问的一件非常糟糕的事情......但是有没有办法根据一系列事实进行选择?

示例:我有一个集合“用户”和该用户的信息,我想说的是,在用户信息内部有一个帖子集合以及该帖子的信息。

Collection('user')->UidUser->InfoUser(Collection('Post')->UidPost->InfoPost, Name, ArrayFllower)

如何使用带有 ArrayFllower 数组的 Post 进行分页?

如果难以理解,我再上例子:

我是用户A,我关注用户B和用户C,我想查看用户B和用户C的帖子,我该怎么办?我想在每次 UserA(我)获得 3 个帖子时分页,然后我向下滚动以自动分页

更新我的问题......

我使用了 2 个查询 => 选择 Array Follow 例如:arrayFollow = [uidUserB, uidUserC]。然后我用for循环Uids来获取他们的Posts,但是为什么我只能获取Array Follow而arrayPost不能呢?

getData = async () => {
        const user = [];
        const Post = [];
        await Promise.all(
            await firebase.firestore().collection('user').doc(Fire.shared.uid).onSnapshot(documentSnapshot => {
                user.push(...documentSnapshot.data().Followings);
            })
        )
       for (const Uid of user) {
            await firebase.firestore().collection('user').doc(Uid).collection('Post').onSnapshot(querySnapshot => {
                querySnapshot.forEach(element => {
                    Post.push({ data: element.data() });
                });
            })
        }
        this.setState({ dataPost: Post });
 }

【问题讨论】:

    标签: firebase react-native google-cloud-firestore


    【解决方案1】:

    我会为您的数据模型选择 SQL 数据库。

    如果您目前正在开发中,我建议您通过创建 2 个collections、用户和帖子来更新您的模型,因为这样更容易选择和查询帖子(使用 UidUser 将用户链接到帖子)。

    如果您未在开发中,您可以创建像云函数这样的服务器端函数,它将遍历 UserA/B/C 的所有帖子并返回第一个 n 帖子,n 是传递给的页码参数云功能。

    如何收集帖子:收集组查询

    1. 在标签中收集您的关注者uid:['uid-A', 'uid-B', 'uid-C']

    2. 收集那些关注用户的所有帖子:

    const querySnapshot = await db.collectionGroup('Post').where('uid', 'in', ['uid-A', 'uid-B', 'uid-C']).get();
    querySnapshot.forEach((post) => {
      // get post data in post.data() :
      console.log(post.id, ' => ', post.data());
    });
    

    【讨论】:

    • '云功能'有代码吗?我还是不明白
    • 我还尝试了 2 个收集帖子和用户,然后我不得不使用 2 个选择,但不知道如何将它们放在一起.....
    • firebase.google.com/docs/functions/http-events 这是 firebase 函数(如 aws lambda)
    • firestore 中是否有用于执行此操作的命令?用两句话就可以了?
    • 好的,我用查询示例更新了我的答案:github.com/firebase/snippets-node/blob/…
    猜你喜欢
    • 1970-01-01
    • 2021-03-27
    • 2021-10-17
    • 1970-01-01
    • 2020-04-01
    • 1970-01-01
    • 2022-01-10
    • 1970-01-01
    相关资源
    最近更新 更多