Firestore array-contains-any 无法正常工作

Sam*_*ath 3 javascript angularfire google-cloud-firestore

"@angular/fire": "5.2.3", 
"firebase": "7.4.0",
Run Code Online (Sandbox Code Playgroud)

注意:members是an array,0,1,2是map数据结构。

在此处输入图片说明

服务.ts

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

页面.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')
    );

  }
Run Code Online (Sandbox Code Playgroud)

没有错误。但它不返回任何值。但它有价值。我的查询有问题吗?

Fra*_*len 9

array-contains-any(以及array-contains)操作者检查阵列中是否包含例如您传递到该呼叫的信息相匹配的元素。因此,在您的情况下,您需要同时传入 theid和 thejoinDateTime才能匹配。

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

如果您不知道joinedDateTime用户的名称,则无法查询当前数据结构中的数组。通常的解决方案是memberIds在文档中存储一个单独的数组字段,它只包含成员的 UID。然后你可以通过查询字段来找到成员:

ref.where('members', 'array-contains', [uid])
Run Code Online (Sandbox Code Playgroud)

或者

ref.where('members', 'array-contains-any', [uid, uid2, uid3])
Run Code Online (Sandbox Code Playgroud)