【发布时间】:2020-11-10 11:12:31
【问题描述】:
使用的堆栈是:
- Firestore
- Firebase 云函数
- 打字稿
- 监听文档更改
我们的每个用户都有一份他收藏的帖子的副本:
user/{userId}/favorites/{postId}
当我们编辑帖子时,会触发一个函数并通过我们的活跃用户迭代以更新收藏夹中具有相同 postId 的帖子。
我的想法是迭代用户
const modifiedPostId = ourModifiedPost.id // from listener
const await activeUsers = firebase.db.collection('users').where('active','==',true).get()
for (const user of activeUsers) {
const await samePostIdUserFavorite = firebase.db.collection(`users/${user.id}/favorites/${modifiedPostId}`).get()
if (samePostIdUserFavorite.exists) {
await copyPostToUserPosts(user, modifiedPostId)
}
}
问题在于只有部分用户收藏了它,因此我们无偿进行大量阅读。
我很难用另一种方式,但我找不到如何限制对活跃用户的搜索,我什至不确定它是否可以工作?!?
const modifiedPostId = ourModifiedPost.id // from listener
// I'm calling a collection but actually get one document in a specific sub-collection from all parent collections...
// I don't see how this could work at all
const sameIdFavorites = firebase.db.collection(`users/{userId}/favorites/${modifiedPostId}`)}.get()
for (const favorite of sameIdFavorites) {
// I have no idea how to get userID
await copyPostToUserPosts(userId, modifiedPostId)
}
有没有办法只感染 'users/{userId}/favorites/{favoriteId}' 的帖子,其中 user.active === true 和 favoriteId === modifiedPostId?
或者在列表中只满足最后一个条件:favoriteId === modifiedPostId而不读取整个用户数据库?
谢谢
【问题讨论】:
标签: typescript firebase google-cloud-firestore nosql google-cloud-functions