我在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; }) ); }) ); }) );
更多推荐
将文档与其子集合中的文档合并
发布评论