某个路径内的collectionGroup [英] collectionGroup within a certain path

查看:43
本文介绍了某个路径内的collectionGroup的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在某个路径内进行一个集合组查询,这意味着我不仅要以collectionId为目标,而且还要以集合的位置为目标.

I'd like to do a collection group query but within a certain path, meaning that I would like to target collections not only with the collectionId but also with where the collection is.

让我们使用文档中使用的示例对其进行更好的解释.我们将在城市中拥有地标,并在他们自己的收藏中拥有一般"地标:

Let's use the example used in the doc to explain it better. We would have landmarks in cities and "general" landmarks in their own collection :

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'
  })
]);

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

Now I would like to query for landmarks that are in a city and not get the general ones. In a simpler way, I would like to do something like this :

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

有可能吗?

推荐答案

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

This is currently not possible with Cloud Firestore. When you perform a collection group query, it will use all of the collections and subcollections with the given name. You are not able to narrow to scope of the query to specific collections.

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

What you can do instead is store a field inside the documents in each subcollection that identify which top-level collection they belong to, then use that field to filter your results.

这篇关于某个路径内的collectionGroup的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆