简体   繁体   English

从文档 Firebase 中获取所有 collections

[英]Get all collections from a document Firebase

I'm building a fullstack todo app using firebase to store user information.我正在使用 firebase 构建一个全栈待办事项应用程序来存储用户信息。 My idea is that each user can create multiple lists and write multiple 'todos' in every list (eg. Have a list for today and another for tomorrow).我的想法是每个用户可以创建多个列表并在每个列表中写入多个“待办事项”(例如,今天有一个列表,明天有另一个列表)。 My data is structured in this manner:我的数据结构如下:

火库查询问题

users (collection) -> usersID (every user is a document) -> todos Lists (collection) -> todoId (document) -> (object with the data) users(集合) -> usersID(每个用户都是一个文档) -> todos 列表(集合) -> todoId(文档) ->(带有数据的对象)

I can't seem to be able to query the multiple lists a user might have (by default, I'm currently just retriving data from the 'todos' collection).我似乎无法查询用户可能拥有的多个列表(默认情况下,我目前只是从“待办事项”集合中检索数据)。 Either I'm failing to see some firestore functionality or I'm not structuring my database correctly.要么我没有看到一些 Firestore 功能,要么我没有正确构建我的数据库。 Any advice?有什么建议吗?

You are structuring your data fine.您正在很好地构建数据。 The problem is that the Firebase Client SDK doesn't offer a function to retrieve all subcollections a document has.问题是Firebase Client SDK不提供 function 来检索文档具有的所有子集合。 To solve your problem you got multiple options.为了解决您的问题,您有多种选择。

1. The cleanest but most exhaustive way 1.最干净但最详尽的方式

You upgrade to the Blaze Plan and use Firebase Functions with the Admin SDK.您升级到Blaze 计划并将 Firebase 函数与管理员 SDK 一起使用。 The Admin SDK offers a function called listCollections() which exactly does what you want.管理员 SDK 提供了一个名为listCollections()的 function,它完全可以满足您的需求。 The disadvantage is that Firebase Functions have something called coldstart which means that when your function was idle for some minutes it takes up to 20 seconds (from my experience) to run again after you called it, which can be really frustrating.缺点是 Firebase 函数有一个叫做coldstart启动的东西,这意味着当你的 function 空闲几分钟时,它需要长达 20 秒(根据我的经验)在你调用它后再次运行,这真的很令人沮丧。 Otherwise you can run a free node.js server on something like Heroku to use the Admin SDK there to avoid coldstart (better option in my opinion).否则,您可以在 Heroku 之类的东西上运行免费的 node.js 服务器来使用管理员 SDK 以避免冷启动(我认为更好的选择)。 The Admin SDK is explicitly not meant to be used on the client side. Admin SDK 明确不打算在客户端使用。 Check this thread 检查这个线程

2. List all your todo lists (easiest way) 2.列出你所有的待办事项(最简单的方法)

Everytime the user creates a new list, you add that to an array in your users document like this:每次用户创建一个新列表时,您都将其添加到用户文档中的数组中,如下所示:

// Representation of your user document
user
    name: "Florian"
    lists: [
        "todos",
        "anotherList"
    ]

So in that way you can just fetch your user, get the lists property from it and call all lists with a loop over the lists array.因此,通过这种方式,您可以获取您的用户,从中获取列表属性,并在lists数组上循环调用所有列表。 As alternative just display the lists content and let the user open it explicitly to save reads.作为替代方案,只需显示lists内容并让用户显式打开它以保存读取。

3. Restructuring your database (I would recommend it) 3. 重构你的数据库(我会推荐它)

If you don't have a lot of data yet, I would recommend to restructure your database like:如果您还没有大量数据,我建议您重组数据库,例如:

users - the collection with all your users (without the todo list subcollections

lists - containing a document for every created list
      - each document contains a field with `owner`
      - each document contains a field with the list name
      - each document owns a subcollection with todos

If you use this way you could just query the database like如果您使用这种方式,您可以像这样查询数据库

const getLists = async () => {
    const ref = collection(db, 'lists');
    const q = query(ref, where('owner', '==', theUserYouWantToQuery);
    // returns array with all list (including listname) of that user.
    const lists = (await getDocs(q)).docs.map(doc => ({ ...doc.data(), doc.id }));
}

when you have all list objects from that user you can either loop over that array to get all todos in the lists or let the user only fetch one list by opening it当您拥有该用户的所有列表对象时,您可以遍历该数组以获取列表中的所有待办事项,或者让用户通过打开它只获取一个列表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Firebase 9 从嵌套的 collections 获取单个文档 - 反应 - Firebase 9 get single document from nested collections - React MongoDB:在一个文档中获取所有集合 - MongoDB: Get all collections in one document 如何从 firebase firestore 异步获取所有父 collections 的所有文档? - How can I get all the documents of a all parent collections asynchronously from firebase firestore? 无法从集合 firebase firestore 中获取所有文档 - Can't get all document from collection firebase firestore 是否可以获取Firebase Cloud Firestore数据库中可用的集合的文档ID? - Is it possible to get document id of collections available in Firebase Cloud Firestore database? Firebase中的Cloud Firestore-获取所有根级别集合 - Cloud Firestore in Firebase - Get all root level collections Firebase 从 collections 和子集合的文档中获取所有文档 - Firebase getting all docs from collections and docs of subcollection 以正确的方式从 Firebase 获取所有 collections 数据 - Getting all collections data from Firebase in the right way 如何从firebase中的不同集合中获取数据? - How to get data from different collections in firebase from react? 有谁知道,为什么我不能从 firebase 集合中获取所有文档? - Does anyone know, why I can´t get ALL document from a firebase Collection?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM