[英]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:你可以简单地链接collection
和doc
直到你得到特定的目标,因为不清楚什么是代码变量和什么是纯文本名称,我假设所有文档和 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.