首页 > 解决方案 > 约会应用 - 我需要在 Firebase Firestore 中使用哪种数据库结构来查询除不喜欢我的用户之外的所有用户?

问题描述

我正在使用React NativeFirebase Cloud Firestore创建约会应用程序。在我的应用程序中,用户可以不喜欢其他用户以将他们排除在下一个查询之外。

我应该使用哪种数据库结构来执行 Firestore 查询


当您不喜欢某人时,用户 ID 将存储在您的用户文档中的一个数组中。Firestore 查询仅提供“array-contains”运算符来根据数组值进行过滤。所以我需要查询“array-NOT-contains”,但 Firestore 不提供它。


我的 Firestore 数据库结构

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

屏幕截图:Firestore 数据库

标签: reactjsfirebasenosqlgoogle-cloud-firestorenative

解决方案


你是对的,where它没有提供像“array_does_not_contain”这样的子句。只需做一个不太合格的查询并从结果中减去。对于广受欢迎的人来说,这可能是一个浪费的查询,但假设大多数人都很可爱……

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))
})

推荐阅读