【问题标题】:Loop through collection and find documents where name equals in firebase functions遍历集合并在firebase函数中查找名称等于的文档
【发布时间】:2021-09-03 14:31:30
【问题描述】:

我正在尝试在 firebase 云函数中创建尽可能多的逻辑,但不知道该怎么做!这是我想要完成的任务:

const myGroups = admin.firestore().collection('groups').find({"playerId":userId});

我知道上面是不正确的,但我似乎无法找到解决方案。我想将我所有的组放入一个数组或其他东西中并将它们发送回我的应用程序。

我不确定这是否明智?!?但是认为从一个地方而不是所有设备添加或更改脚本会更容易。

【问题讨论】:

    标签: node.js firebase google-cloud-firestore google-cloud-functions


    【解决方案1】:

    您好,您需要使用 firestore 复合查询,您可以在此处找到文档:https://firebase.google.com/docs/firestore/query-data/queries

    所以在你的情况下,代码看起来像:

    const myGroups = await admin.firestore().collection('groups').where("playerId", "==", userId}).get();

    【讨论】:

      【解决方案2】:

      您可以在CollectionReference 上使用get() 获取所有文档。

      const myGroups = await admin.firestore().collection('groups').get()
      
      return myGroups.docs.map(group => ({id: group.id, ...group.data()}))
      

      此外,如果您要查找playerIduserId 的组。然后你可以使用where() 子句来缩小搜索结果:

      const myGroups = await admin.firestore()
        .collection('groups')
        .where("playerId", "==", userId)
        .get()
      

      您可以在documentation 中阅读有关查询的更多信息。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-19
        • 2012-06-02
        • 1970-01-01
        • 1970-01-01
        • 2017-04-15
        • 1970-01-01
        相关资源
        最近更新 更多