【发布时间】:2023-03-25 09:03:01
【问题描述】:
【问题讨论】:
标签: android firebase kotlin google-cloud-firestore
【问题讨论】:
标签: android firebase kotlin google-cloud-firestore
我想在一个查询中列出这两个集合的完整列表
不,实际上你不能那样做。 Firestore 中的查询很浅,这意味着您只能从运行查询的集合中获取文档。无法在单个查询中从两个顶级集合中获取文档。 Firestore 不支持一次性跨不同集合进行查询。
如果您的要求是只查询一次数据库以获取两个集合的内容,那么您应该考虑通过创建一个包含两个集合中的项目的单个集合来更改数据库架构:
Firestore-root
|
--- items (collection)
|
--- $itemId (document)
| |
| --- type: "purchase"
| |
| --- //Rest of the fields
|
--- $itemId (document)
|
--- type: "sale"
|
--- //Rest of the fields
通过这种方式,您可以一次获得所有物品。但是,如果您需要区分它们,那么您应该使用查询。例如,如果您只想获取所有待售商品,则可以使用以下查询:
FirebaseFirestore rootRef = FirebaseFirestore.getInstance()
CollectionReference itemsRef = rootRef.collection("items")
Query qyeryItemsForSale = itemsRef.whereEqualTo("type", "sale")
【讨论】: