2
votes

I'm creating a dating app with React Native and Firebase Cloud Firestore. In my app users can dislike other users to exclude them from the next query.

Which database structure should I use to do the Firestore query?


When you dislike someone, the userID will be stored in an array inside your user document. The Firestore query only provides 'array-contains' operator to filter based on array values. So I need to query "array-NOT-contains" but Firestore doesn't provide it.


my Firestore Database structure:

collection('user').doc('userID').match.dislike[dislikedUserID1, dislikedUserID2,...]

screenshot: Firestore Database

1

1 Answers

2
votes

You're right that where doesn't offer a clause like "array_does_not_contain". Just do a less qualified query and subtract from the results. This could be a wasteful query for someone who is widely disliked, but assuming most people are adorable...

const adorableMe = firebase.auth().currentUser
collection('user').where(/* they might think I'm a cutie pie */).get().then(snapshot => {
    // filter result to exclude the (tiny!) set of users who don't fancy me
    return snapshot.docs.filter(doc => !doc.data().dislikes.includes(adorableMe.id))
})