array包含在subCollection中

时间:2020-09-22 12:42:31

标签: firebase google-cloud-firestore

似乎无法理解arrayContains是否适用于Firestore中的子集合。

此代码不返回任何数据

Firestore.firestore()
  .collection("all_chatrooms")
  .whereField("postmoderators", arrayContains: userId)

但是指定目标收集路径却可以...

Firestore.firestore()
  .collection("all_chatrooms")
  .document("randomId")
  .collection("chatroom")
  .whereField("postmoderators", arrayContains: userId)

是否可以在不指定documentId的情况下在subCollections中搜索数组?

您是否可能必须在地图中存储userId才能在subColletions中搜索?

1 个答案:

答案 0 :(得分:1)

Firestore查询很浅,这意味着当您查询特定的集合“ C1”时,仅返回该集合中的文档,而不返回“ C1”中文档的子集合中的文档。

但是,在您的情况下,Collection Group queries可能“来了”。如文档所示:

一个集合组由具有相同ID的所有集合组成。对于 例如,如果您的城市集中的每个文档都有一个 子集称为地标,所有地标子集 属于同一收集组。

因此,如果all_chatrooms集合中的所有文档都有一个名为chatroom的子集合,则可以按以下方式使用集合组查询:

Firestore.firestore().collectionGroup("chatroom").whereField("postmoderators", arrayContains: userId)

当然,这样做您将获得userId集合中所有文档的所有postmoderators子集合中的所有文档(在chatroom中带有all_chatrooms

相关问题