firebase - 某个路径内的collectionGroup

标签 firebase google-cloud-firestore

我想做一个集合组查询,但在某个路径内,这意味着我不仅希望使用 collectionId 还希望使用集合所在的位置来定位集合。

让我们使用文档中使用的示例来更好地解释它。我们将在他们自己的收藏中拥有城市中的地标和“一般”地标:

let citiesRef = db.collection('cities');

let landmarks = Promise.all([
  citiesRef.doc('SF').collection('landmarks').doc().set({
    name: 'Golden Gate Bridge',
    type: 'bridge'
  }),
  citiesRef.doc('SF').collection('landmarks').doc().set({
    name: 'Legion of Honor',
    type: 'museum'
  }),
  citiesRef.doc('LA').collection('landmarks').doc().set({
    name: 'Griffith Park',
    type: 'park'
  }),
  citiesRef.doc('LA').collection('landmarks').doc().set({
    name: 'The Getty',
    type: 'museum'
  }),
  citiesRef.doc('DC').collection('landmarks').doc().set({
    name: 'Lincoln Memorial',
    type: 'memorial'
  })
]);

let generalLandmarks = Promise.all([
  db.collection('landmarks').doc().set({
    name: 'National Air and Space Museum',
    type: 'museum'
  }),
  db.collection('landmarks').doc().set({
    name: 'Ueno Park',
    type: 'park'
  }),
  db.collection('landmarks').doc().set({
    name: 'National Museum of Nature and Science',
    type: 'museum'
  }),
  db.collection('landmarks').doc().set({
    name: 'Jingshan Park',
    type: 'park'
  }),
  db.collection('landmarks').doc().set({ 
    name: 'Beijing Ancient Observatory',
    type: 'museum'
  })
]);

现在我想查询城市中的地标而不是一般的地标。以更简单的方式,我想做这样的事情:

let museums = db.collection('cities').collectionGroup('landmarks').where('type', '==', 'museum');

是否可以 ?

最佳答案

Cloud Firestore 目前无法做到这一点。当您执行集合组查询时,它将使用具有给定名称的所有集合和子集合。您无法将查询范围缩小到特定集合。

您可以做的是在每个子集合的文档中存储一个字段,用于标识它们属于哪个顶级集合,然后使用该字段来过滤您的结果。

关于firebase - 某个路径内的collectionGroup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56934947/

相关文章:

javascript - 如何在 React 中获取 Firestore 的文档 ID

javascript - 使用 URL 从 Firestore 检索文档

swift - 覆盖变量中的值

java - 为什么不在 Android 控制台上显示 firebase 事件?

Firebase 客户端对实时数据库的访问权限将在 X 天后到期

firebase - 如何调试 firestore.rules 变量和函数?

swift 'FIRInvalidArgumentException' ,原因 : 'Unsupported type: NSURL (found in field AccountTypeImageURL)'

firebase - 无法解决 Firestore 依赖性

android - 如何只显示一次 Firebase 数据?

ios - 可以将 Analytics 划分为 DEBUG 和 RELEASE 吗?