将文档与其子集合中的文档合并

编程入门 行业动态 更新时间:2024-10-28 00:28:01
本文介绍了将文档与其子集合中的文档合并的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在firestore下有一个具有以下结构的数据库:

I have a database under firestore with the following structure:

->聊天室 ->用户

-> Chat Room -> Users

我有一个聊天室"(ChatRoom)集合,其中包含一个用户"(Users)集合.在用户集合中,每个文档都包含"read:true/false"字段,以了解用户是否已阅读会议室中的消息.

I have a "ChatRoom" collection that contains a "Users" collection. In the users collection each document contains the field "read: true/false" in order to know if the user has read the messages that are in the room.

要检索当前用户的房间,请使用以下代码:

To retrieve the rooms of the current user I use this code:

getRoomFromUserId(userId: string) { let rooms$: Observable<any>; let rooms: AngularFirestoreCollection<any>; rooms = this.afs.collection('ChatRoom', ref => { return ref.where('Chatter.' + userId, '==', true); }); rooms$ = rooms.snapshotChanges().map(changes => { return changes.map(a => { const data = a.payload.doc.data(); const id = a.payload.doc.id; return {id, ...data}; }); }); return rooms$; }

要从用户"子集合中恢复数据,我使用以下代码行:

To recover data from the "Users" subcollection I use this line of code:

this.afs.collection('ChatRoom').doc(RoomID).collection('Users').doc(UserId);

我想检索一个包含房间数据和每个房间"read:true/false"的对象,我认为使用可观察对象是可行的,但我不知道该怎么做.您有解决方案的想法吗?

I'd like to retrieve an object that contains the room data and the "read: true/false" for each room I think it's possible with observables but I don't know how to do it. Do you have any ideas for a solution?

推荐答案

我终于知道了.为了将用户子集合链接到会议室文档,链接到CombineLatest运算符的mergeMap运算符允许知道相关用户是否阅读了会议室消息.

I finally figured it out. To link the users sub-collections to the room document the mergeMap operator linked to the combineLatest operator allows to know if the user in question read the room messages.

let rooms: AngularFirestoreCollection<any>; rooms = this.afs.collection('ChatRoom', ref => { return ref.where('Chatter.' + userId, '==', true); }); let readRoom$: Observable<any>; let readRoom: AngularFirestoreCollection<any>; return rooms.snapshotChanges().pipe( mergeMap(changes => { return ObservablebineLatest(changes.map(a => { const data = a.payload.doc.data(); const id = a.payload.doc.id; let roomReturn = {id, readMessage: '', photoProfile: '', ...data}; readRoom = this.afs.collection('ChatRoom').doc(id).collection('Users'); readRoom$ = readRoom.snapshotChanges(); return readRoom$.pipe( map(userInRoom => { userInRoom.map(userList => { if (userList.payload.doc.id === userId) { roomReturn.readMessage = userList.payload.doc.data().ReadMessage; } else { roomReturn.photoProfile = userList.payload.doc.data().PhotoProfile; } }); return roomReturn; }) ); }) ); }) );

更多推荐

将文档与其子集合中的文档合并

本文发布于:2023-11-22 20:21:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1618878.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文档   其子

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!