Python 在消息集合-Firestore中循环时从一个用户集合获取信息
我的数据库由一个用户集合和一个消息集合构成(见下文)。应用程序中只有1-2-1条消息(无聊天室)。消息集合的文档名为user1:user2 我正在尝试在我的应用程序中创建一个页面,用户可以看到与他们聊天的所有用户,然后可以单击每个聊天,并打开与该用户的消息链。我相信我知道如何创建第二部分(消息链),但我不确定如何收回用户当前的所有聊天记录 到目前为止,我已经Python 在消息集合-Firestore中循环时从一个用户集合获取信息,python,google-cloud-firestore,Python,Google Cloud Firestore,我的数据库由一个用户集合和一个消息集合构成(见下文)。应用程序中只有1-2-1条消息(无聊天室)。消息集合的文档名为user1:user2 我正在尝试在我的应用程序中创建一个页面,用户可以看到与他们聊天的所有用户,然后可以单击每个聊天,并打开与该用户的消息链。我相信我知道如何创建第二部分(消息链),但我不确定如何收回用户当前的所有聊天记录 到目前为止,我已经 def load_chats(self): messages = self.my_firestore.db.col
def load_chats(self):
messages = self.my_firestore.db.collection(u'messages').stream()
#self.users = self.my_firestore.db.collection(u'users').stream()
for doc in messages:
if self.local_id in doc.id: #This is checking if "user1" is in "user1:user2", for example
chat_messages.add_widget(ChatButton) #Adds a new button each time it finds "user1" in the doc.id within messages
在这个for循环中,当我为聊天创建按钮时,我需要来自users
集合的信息,以便显示聊天对象的姓名
但是,我不知道如何做到这一点——除了在消息中的for doc中放入for循环之外,它循环通过self.users
并返回与“user2”匹配的用户信息。这将创造大量的阅读,所以我甚至不娱乐我的疯狂想法
非常感谢您的帮助,如果不清楚,请告诉我。您可以在聊天组中添加一个参与者
数组字段:
data = {
'GroupId': 'userOneID:userTwoID',
'Participants': ['userOneID', 'userTwoID']
}
然后,您可以在聊天组中查询用户为userOne
或userTwo
的聊天组:
chatGroups = self.my_firestore.db.collection(u'messages') \
.where('Participants', 'array_contains', 'userID') \
.stream()
您可以在聊天组中添加参与者数组字段:
data = {
'GroupId': 'userOneID:userTwoID',
'Participants': ['userOneID', 'userTwoID']
}
然后,您可以在聊天组中查询用户为userOne
或userTwo
的聊天组:
chatGroups = self.my_firestore.db.collection(u'messages') \
.where('Participants', 'array_contains', 'userID') \
.stream()
谢谢,这会比这个解决方案使用更少的读取:def user\u messages(self):messages=self.my\u firestore.db.collection(u'messages').stream()user\u messages=self.root.get\u screen(“user\u messages”).ids['user\u messages\u layout']对于消息中的文档:if self.local\u id in doc.id:user\u id=doc.id.split(“:”[1]user=self.my\u firestore.db.collection(u'users').document(user\u id.get()
我最初是这样创建的,但是如果您的建议使用更少的读取次数(或者通常更有效)我将改为您所说的。由于格式不好,我不知道如何在注释中正确添加代码是的,where
过滤器可以避免每次加载整个邮件集。谢谢,这会比此解决方案使用更少的读数:def user\u messages(self):messages=self.my\u firestore.db.collection(u'messages').stream()user\u messages=self.root.get\u screen(“user\u messages”).ids['user\u messages\u layout']用于消息中的文档:if self.local\u id in doc.id:user\u id=doc.id.split(“:”[1]user=self.my\u firestore.db.collection(u'users')).document(user_id).get()
我最初是这样创建的,但如果您的建议使用更少的读取次数(或通常更有效),我将改为您所说的格式。由于格式不佳,我不确定如何在注释中正确添加代码是,where
过滤器可避免每次加载整个消息。