Firestore:查询集合是否也包括其子集合? [英] Firestore: Does querying a collection also includes their sub collection?

查看:31
本文介绍了Firestore:查询集合是否也包括其子集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 users 的集合,其中包含他们的姓名和其他个人详细信息,它还有一个包含他们的车辆的子集合.

I've a collection called users consisting their name and other personal details, and it also have a sub collection consisting of their vehicles.

但是对于某些操作,我只想查询用户数据.恐怕它也会包括子集合,这会增加我的数据使用量.

But for some operation, I want to query only the user data. I'm afraid that it would include the sub collection also, which will increase my data usage.

数据库结构如下图:

db.collection("users").limit(10)
.get()
.then(function(querySnapshot) {     
  querySnapshot.forEach(function(doc) {
    console.log(doc.data());
     $scope.userList.push(doc.data());           
  });           
})
.catch(function(error) {
  console.log("Error getting documents: ", error);
});

我正在获取数据,但我不确定这是否是正确的查询方式.

I'm getting the data but i'm not sure whether this is the correct way of querying or not.

推荐答案

与在 Firebase 实时数据库中显示用户列表不同,您将下载整个 User 对象以及所有子项存在于该对象中,在 Cloud Firestore 中这不再是问题.因此,如果文档中有子集合,则不会下载它们.

Unlike in Firebase realtime database where to display a list of users you would have been downloaded the entire User object together with all children that exist within that object, in Cloud Firestore this is not an issue anymore. So if you have subcollections within a document, you will not download them.

Firestore 中的查询很浅:它们只从运行查询的集合中获取项目.无法在单个查询中从顶级集合和其他集合或子集合中获取文档.所以不用担心这些子集合.

Queries in Firestore are shallow: they only get items from the collection that the query is run against. There is no way to get documents from a top-level collection and other collections or subcollections in a single query. So don't worry about those subcollections.

这篇关于Firestore:查询集合是否也包括其子集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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