简体   繁体   English

Query Document 在 Flutter 的 Firestore 中获取特定 collections 的快照

[英]Query Document to get snapshots from specific collections in Firestore in Flutter

I am using Firebase Cloud Firestore as the database for my app.我正在使用 Firebase Cloud Firestore 作为我的应用程序的数据库。 The data structure is as below数据结构如下

expenses (collection)费用(收取)

     |____[userId] (doc)

                 |___[year-month] collection

                                |___[expenseId] doc

                                               |______expense fields(amount, date)

How can I get all expenses from a [userId] doc for specific [year-month] collections.如何从 [userId] 文档中获取特定 [year-month] collections 的所有费用。

Is there a way to query a doc like we have.where for collection有没有办法像我们一样查询文档。在哪里收集

You have nested collections in your database, right?你在你的数据库中嵌套了 collections,对吧? you can simply chain collection and doc until you get the specific target, since it's not clear what is code variables and what is plain text names, I will assume that all documents and collections names are text, just to so you get the idea:你可以简单地链接collectiondoc直到你得到特定的目标,因为不清楚什么是代码变量和什么是纯文本名称,我假设所有文档和 collections 名称都是文本,只是为了让你明白:

// This is the reference of a the "year-month" expenses of the user.
Query query = FirebaseFirestore.instance.collection('expenses').doc("userId").collection("year-month");

this will get you a QuerySnapshot containing all documents inside the collection with that "year-month":这将为您提供一个QuerySnapshot ,其中包含带有该“年-月”的集合中的所有文档:

QuerySnapshot = await query.get()

You can also get a specific document in that collection like this:您还可以像这样在该集合中获取特定文档:

DocumentReference doc = await query.doc("expenseId").get(); 

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

相关问题 从 flutter firestore 中的子集合中获取特定文档 - fetching a specific document from a subcollection in flutter firestore 从 Flutter 中的 Firestore 查询单个文档(cloud_firestore Plugin) - Query a single document from Firestore in Flutter (cloud_firestore Plugin) 如何获取从 Flutter 中的 Firebase Firestore 检索到的文档列表中存在的特定文档的索引? - How to get the index of a specific document present in the list of documents retrieved from Firebase Firestore in Flutter? 如何从 Flutter 中的 firestore 文档中获取文档 ID? - How to get the document id from a firestore document in Flutter? 无法从 flutter Cloud firestore 获取一份文件 - Can't get one document from flutter cloud firestore 从 firestore 中的文档集合和文档子集合查询获取数据 - get data with query from document collection and document subcollection in firestore 从 flutter 监听 firestore 数据库中的两个 collections - Listen to two collections in firestore database from flutter 如何从 flutter 中的 firestore 文档字段获取数据? - How to get data from firestore document fields in flutter? 如何从 Firbase Firestore 获取 Flutter 中的最后一个文档? - How to get Last document in Flutter from Firbase Firestore? 如何通过文档 ID 从 firestore 网站获取特定字段数据 - How get specific Field data by document id from firestore website
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM