firebase 中的 OR 子句 java android

OR clause in firebase java android

有谁知道我如何在 firebase 的 where 子句中做一个常见的 "OR"?

我需要在查询中这样做,因为我正在将查询发送到适配器。所以,我的意思是,我不能添加一个侦听器并检查一个值然后检查另一个值。我需要在我的查询中有指向该结果的完整查询。

我有的是这样的:

chat1:
   user1Id: "1"
   user2Id: "2"
   bothUsers: "1_2"

chat2:
   user1Id: "2"
   user2Id: "4"
   bothUsers: "2_4"

我需要获取id为“2”的用户的所有聊天记录。我正在尝试执行如下查询:

userLogged = 2;
Query queryRef = firebase.orderByChild("user2Id").equalTo(userLogged);

然而,只有当用户 2 位于 user2Id 位置时,它才会获取聊天记录。但是在上面的示例中,当用户 2 在 user1Id (chat2) 中时,它不会获取。我需要得到它。我该怎么做?

在 NoSQL 数据库中,您通常需要根据应用程序的需要对数据进行建模(有关 NoSQL Data Modeling 的详细解释,请参阅本文)。因此,如果您需要特定用户的所有聊天列表,请存储该列表:

/userChats
  user1Id
    chat1: true
  user2Id
    chat1: true
    chat2: true
  user4Id
    chat2: true

此过程称为反规范化,我在上面链接的文章 Firebase documentation on structuring data and in this blog post.

中对此进行了介绍