0
鉴于以下结构,我如何才能检索特定用户通知?Firebase数据库:查询非规范化数据挣扎
即:uid0001
768,16能够检索列表:[xxxxx01, xxxxx02]
:
notifications
uid01
xxxxx01
uid02
xxxxx02
uid03
xxxxx03
uid04
xxxxx04
users
uid0001
notifications
uid01
uid02
不幸的是,这个怪物是极其低效:
firebase.database().ref(`users/${user.userId}/notifications`).once('value').then((snapshot) => {
snapshot.forEach((data) => {
firebase.database().ref(`notifications/${data.key}`).once('value').then((snap) => {
this.list.unshift(localUpdates)
})
})
})
可能沿(警告,伪代码的东西线即将到来):
firebase.database().ref('notifications').on('child_added').equalTo('whatever the childs key is here').then((snapshot) => {
})
As always任何方向,欢呼欢呼和腐烂的水果,谢谢!
这件事我一直考虑。 'for_user'需要是一个数组。 'queryOrdered'仍然可以像任何工作吗?对于实例:'''queryOdered(byChild:“for_user”)。contains(“uid01”)''' – studiobrain
@studiobrain号请不要在Firebase中使用或存储数组!参见[数组是邪恶的](https://firebase.googleblog.com/2014/04/best-practices-arrays-in-firebase.html)。对于我提出的结构,无论如何都没有理由使用数组。 Firebase中没有“包含”,这就是为什么我在我的解决方案中包含选项,以便您可以通过多种方式查询数据。 – Jay
一致认为,这不仅仅意味着一个字面数组只是一个列表。上述将工作得很好,我只是深入一层,以获得基于用户的关联通知...谢谢! – studiobrain