如何使用React-Native在Firestore中获取包含包含特定字符串的数组的文档

时间:2018-09-17 22:15:28

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

我刚开始使用Firebase,正在尝试确定如何最好地构建Firestore数据库。

我要从“事件”集合中查找所有文档,其中“参与者”(每个事件文档上的数组字段,其中包含具有键“ displayName”和“ uid”的对象)包含至少一个匹配的uid。 。我要比较的uid列表将是用户的朋友。

因此,从语义上讲,我想使用事件参与者和用户朋友的uid查找该事件的参与者中至少有一个是“朋友”的所有事件。

希望我没有失去你。也许此屏幕截图会有所帮助。

这就是我现在如何设计“事件”系列的方式 enter image description here

这样对Firestore进行深层查询是否可行?还是我需要在客户端进行过滤?

编辑-添加代码

      // TODO: filter events to only those containing friends

        // first get current users friend list
        firebase.firestore().doc(`users/${this.props.currentUser.uid}`)
        .get()
        .then(doc => {
            return doc.data().friends
        }) 
        .then(friends => { // 'friends' is array of uid's here
        // query events from firestore where participants contains first friend
        // note: I plan on changing this design so that it checks participants array for ALL friends rather than just first index. 
        // but this is just a test to get it working...
            firebase.firestore().collection("events").where("participants", "array-contains", friends[0])
            .get()
            .then(events => {
                // this is giving me ALL events rather than 
                // filtering by friend uid which is what I'd expect
                console.log(events)
                // update state with updateEvents()
                //this.props.dispatch(updateEvents(events))
            })
        })

我正在使用React-Native-Firebase

"react-native": "^0.55.0",
"react-native-firebase": "^4.3.8",

1 个答案:

答案 0 :(得分:0)

能够通过执行@Neelavar所说的然后更改我的代码,使其在第一级集合查询中链接then()来解决这个问题。

            // first get current users' friend list
            firebase.firestore().doc(`users/${this.props.currentUser.uid}`)
            .get()
            .then(doc => {
                return doc.data().friends
            })
            // then search the participants sub collection of the event
            .then(friends => {
                firebase.firestore().collection('events')
                .get()
                .then(eventsSnapshot => {
                    eventsSnapshot.forEach(doc => {
                        const { type, date, event_author, comment } = doc.data();
                        let event = {
                            doc, 
                            id: doc.id,
                            type,
                            event_author,
                            participants: [],
                            date,
                            comment,
                        }
                        firebase.firestore().collection('events').doc(doc.id).collection('participants')
                        .get()
                        .then(participantsSnapshot => {
                            for(let i=0; i<participantsSnapshot.size;i++) {
                                if(participantsSnapshot.docs[i].exists) {
                                    // if participant uid is in friends array, add event to events array
                                    if(friends.includes(participantsSnapshot.docs[i].data().uid)) {
                                        // add participant to event
                                        let { displayName, uid } = participantsSnapshot.docs[i].data();
                                        let participant = { displayName, uid }
                                        event['participants'].push(participant)
                                        events.push(event)
                                        break;
                                    }
                                }
                            }
                        })
                        .then(() => {
                            console.log(events)
                            this.props.dispatch(updateEvents(events))
                        })
                        .catch(e => {console.error(e)})
                    })
                })
                .catch(e => {console.error(e)})
            })