9
"@angular/fire": "5.2.3", 
"firebase": "7.4.0",

Note: members is an array and 0,1,2 is map data structures.

enter image description here

service.ts

 getUserChatGroups(uid: string): Observable<DocumentChangeAction<MessageGroupMemberModel>[]> {
    return this.afs.collection<MessageGroupMemberModel>(`messageGroups`, ref => ref.where('members', 'array-contains-any', [uid])).snapshotChanges();
  }

page.ts

 init(): void {
    const uid: string = this.authService.getUserUid();
    this.chatService.getUserChatGroups(uid).subscribe(res => {
      console.log(res);
    },
      err => console.log(err),
      () => console.log('request completed')
    );

  }

No errors. But it doesn't return any value. But it has values. Is there something wrong with my query?

halfer
  • 19,824
  • 17
  • 99
  • 186
Sampath
  • 63,341
  • 64
  • 307
  • 441

1 Answers1

16

The array-contains-any (and also the array-contains) operator check whether the array contains an element that example matches the information that you pass into the call. So in your case, you need to pass in both the id and the joinDateTime in order to match.

ref.where('members', 'array-contains-any', [{ id: uid, joinedDateTime: ... }])

If you don't know the joinedDateTime for the user, you can't query the array in your current data structure. The normal solution is to store a separate array field memberIds in the document too, which contains just the UIDs of the members. Then you can find the members by querying on that field:

ref.where('members', 'array-contains', [uid])

Or

ref.where('members', 'array-contains-any', [uid, uid2, uid3])
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Is it suitable for big arrays? If it's not, Do you have any suggestions? In the documentation, it says up to 10 equality https://firebase.google.com/docs/firestore/query-data/queries#in_and_array-contains-any – cipli onat Mar 01 '20 at 07:12
  • not-in supports up to 10 comparison values. The sum of filters, sort orders, and parent document path (1 for a subcollection, 0 for a root collection) in a query cannot exceed 100. This is calculated based on the disjunctive normal form of the query. – behicsakar Aug 31 '23 at 12:38