简体   繁体   English

firestore Query issue order by 如果数组包含

[英]firestore Query issue order by if array contains

I have a group chat app.我有一个群聊应用程序。 Each chat document has a node called每个聊天文档都有一个名为

chatUsers:[1,2,3,4,5]聊天用户:[1,2,3,4,5]

where 1-5 is a user Id on that node.其中 1-5 是该节点上的用户 ID。

I need to pull all chats where I am a user, so I use the array-contains operator.我需要在我作为用户的地方提取所有聊天记录,因此我使用数组包含运算符。 My issue is there is also another node called archivedChat.我的问题是还有另一个名为 archivedChat 的节点。 That node tells if I archived the chat.该节点告诉我是否存档了聊天。

ie: IE:

archivedChat:[1,2] meaning users 1 and 2 have archived this chat. archivedChat:[1,2] 表示用户 1 和 2 已将此聊天存档。 I want to get all chats where I am a user and I have not archived, and then all chats I am a user and have archived.我想获取所有我是用户且未存档的聊天记录,然后是我是用户且已存档的所有聊天记录。

firebase prevents using these two operators together, and I understand I can filter on the front end, but I'd need all records retrieved that. firebase 阻止同时使用这两个运算符,我知道我可以在前端进行过滤,但我需要检索所有记录。 I could have 1000 chat rooms/documents, so I do not want to query the entire collection, I'd much much prefer doing 2 separate queries.我可以有 1000 个聊天室/文档,所以我不想查询整个集合,我更愿意做 2 个单独的查询。 Here is where I am at:这是我所在的位置:

 query(
        roomsRef,
        where(USERS_PATH, 'array-contains', currentUserId),
        where(ARCHIVE_USERS_FIELD, 'not-in', [[currentUserId]]),
        orderBy(LAST_UPDATED_FIELD, 'desc'),
        limit(roomsPerPage),
        startAfter(lastRoom)

I can think of no way to do this.我想不出办法做到这一点。 Since the chat is the same whether it is archived or not, and my archived flag just shows all archived chats in a different area and effects how I display it, I really do not want to move it into another collection....由于聊天无论是否存档都是一样的,而我的存档标志只是在不同区域显示所有存档聊天并影响我如何显示它,我真的不想将它移到另一个集合中......

Any help?有什么帮助吗?

I'd recommend adding a third field that essentially combines the information from the other user lists.我建议添加第三个字段,它基本上结合了来自其他用户列表的信息。 If you only want to show the document for users that are in the USERS_PATH and are not in ARCHIVE_USERS_FIELD , add a field (say SHOW_USERS ) that contains just the UIDs of those users.如果您只想为USERS_PATH中而不是ARCHIVE_USERS_FIELD中的用户显示文档,请添加一个仅包含这些用户的 UID 的字段(例如SHOW_USERS )。

This type of data duplication is quite common when using NoSQL databases, where you often have to model/augment your data to fit with the specific use-cases you have.这种类型的数据重复在使用 NoSQL 数据库时非常常见,您经常需要对数据进行建模/扩充以适应您的特定用例。

After learning the limitations, i think i am going to go to mongodb cloud and ditch firestore.了解限制后,我想我要去 go 到 mongodb 云和沟渠 firestore。

Firestore is very good, but if you want complex queries ie(users are part of teams, and we want both team chats to be pulled along with user chats, team chats a user is not on, user chats archived, and team chats archived from a users view, there is no great way to query. Firestore 非常好,但如果你想要复杂的查询,即(用户是团队的一部分,我们希望两个团队聊天都与用户聊天一起被拉出,用户不在的团队聊天,用户聊天存档,团队聊天存档来自a users view,没有很好的查询方式。

Lastly if i could have 1000 team Chats all with listeners on the room user online status's, etc i can easily exceed quota limits.最后,如果我可以在房间用户在线状态等方面与听众进行 1000 次团队聊天,我很容易超过配额限制。

Mongodb requires a server layer but there are no server limits, and better query capabilities. Mongodb需要服务器层但没有服务器限制,查询能力更好。

Much more complex to build as subscribing to documents has to be pushed via sockets instead of a very clean front-end sdk that firestore has.构建起来要复杂得多,因为必须通过 sockets 推送文档订阅,而不是 firestore 拥有的非常干净的前端 sdk。

Each has their perks, but a large scale chat app with 1000+ rooms and complex querying feels like forcing a square peg through a round hole here:(每个人都有自己的好处,但是一个拥有 1000 多个房间和复杂查询的大型聊天应用程序感觉就像在此处将方钉从圆孔中强行插入:(

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM