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

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

问题描述

我有一个名为用户的集合,其中包括他们的姓名和其他个人详细信息,并且还有一个子集合,其中包括他们的车辆.

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天全站免登陆