遍历集合并在 firebase 函数中查找名称等于的文档
Loop through collection and find documents where name equals in firebase functions
我正尝试在 firebase 云函数中创建尽可能多的逻辑,但不确定如何操作!这是我想要完成的:
const myGroups = admin.firestore().collection('groups').find({"playerId":userId});
我知道上面的说法不正确,但我似乎找不到解决办法。我想将我所有的组放入一个数组或其他东西中,然后将它们发送回我的应用程序。
我不确定这是否明智?!?但是认为从一个地方而不是所有设备添加或更改脚本会更容易。
您可以在 CollectionReference
上使用 get()
来获取所有文档。
const myGroups = await admin.firestore().collection('groups').get()
return myGroups.docs.map(group => ({id: group.id, ...group.data()}))
此外,如果您要查找 playerId
为 userId
的组。然后你可以使用 where()
子句来缩小搜索结果:
const myGroups = await admin.firestore()
.collection('groups')
.where("playerId", "==", userId)
.get()
您可以在 documentation 中阅读有关查询的更多信息。
您好,您需要使用 firestore 复合查询,您可以在此处找到文档:https://firebase.google.com/docs/firestore/query-data/queries
因此在您的情况下,代码将类似于:
const myGroups = await admin.firestore().collection('groups').where("playerId", "==", userId}).get();
我正尝试在 firebase 云函数中创建尽可能多的逻辑,但不确定如何操作!这是我想要完成的:
const myGroups = admin.firestore().collection('groups').find({"playerId":userId});
我知道上面的说法不正确,但我似乎找不到解决办法。我想将我所有的组放入一个数组或其他东西中,然后将它们发送回我的应用程序。
我不确定这是否明智?!?但是认为从一个地方而不是所有设备添加或更改脚本会更容易。
您可以在 CollectionReference
上使用 get()
来获取所有文档。
const myGroups = await admin.firestore().collection('groups').get()
return myGroups.docs.map(group => ({id: group.id, ...group.data()}))
此外,如果您要查找 playerId
为 userId
的组。然后你可以使用 where()
子句来缩小搜索结果:
const myGroups = await admin.firestore()
.collection('groups')
.where("playerId", "==", userId)
.get()
您可以在 documentation 中阅读有关查询的更多信息。
您好,您需要使用 firestore 复合查询,您可以在此处找到文档:https://firebase.google.com/docs/firestore/query-data/queries
因此在您的情况下,代码将类似于:
const myGroups = await admin.firestore().collection('groups').where("playerId", "==", userId}).get();