如何使用 dart 编程语言从 firebase 集合返回文档引用?
我正在使用餐厅集合来存储所有餐厅信息。文档 ID 代表一家独特的餐厅。该文档包含更多集合(订单、项目等)。订单集合包含与用户 UID 相同的文档。文档内部包含一个名为“All Orders”的集合,用于保存该特定用户的所有订单。
如果用户从该餐厅购买了任何东西,我想返回餐厅文档引用 ID。然后返回转换为字符串的文档引用类型列表。
这是我到目前为止所拥有的:
Future<List<String>> getResDocIDS() async {
List<String> ids = ["none"];
DocumentReference collectionDoc = _firestore.collection("Restaurant").where(
_auth.currentUser!.uid,
isEqualTo: _firestore
.collection("Restaurant")
.doc()
.collection("Orders")
.doc(_auth.currentUser!.uid))
return ids;
}
I am using a Restaurant collection to store all restaurants information. The document ID represent a unique restaurant. The document contain more collections (orders, items etc.). The orders collection contain documents that are same as the user UID. Inside the document contain a collection call "All Orders" that saves all the orders of that particular user.
I want to return the Restaurant document refence ids if the user purchased anything from that restaurants. Then return a list of document references type casted to string.
Here is what I have so far:
Future<List<String>> getResDocIDS() async {
List<String> ids = ["none"];
DocumentReference collectionDoc = _firestore.collection("Restaurant").where(
_auth.currentUser!.uid,
isEqualTo: _firestore
.collection("Restaurant")
.doc()
.collection("Orders")
.doc(_auth.currentUser!.uid))
return ids;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Firestore 查询只能针对该查询返回的数据设置条件。因此,无法使用每个餐厅的
Orders
子集合的条件对Restaurant
运行查询。如果您想要搜索所有
Orders
子集合,则需要使用集合组查询。然后,根据生成的订单,您可以确定父餐厅文档参考,并在需要时加载这些文档。或者,您可以向每家餐厅添加一个
orderingUsers
字段,用于跟踪从该餐厅点餐的用户的 UID,但在这种情况下,您必须密切关注文档的大小。Firestore queries can only have conditions on data that is returned by that query. So there's no way to run a query on
Restaurant
with a condition on each restaurant'sOrders
subcollection.If you want to search across all
Orders
subcollections, you'll need to use a collection group query. Then from the resulting orders, you can determine the parent restaurant document reference, and if needed load those documents.Alternatively, you can add a
orderingUsers
field to each restaurant where you track the UID of users who ordered from that restaurant, but you'll have to keep an eye on the size of the document in that case.