简体   繁体   English

使用 Flutter 的 Firestore 组集合查询

[英]Firestore group collection query using Flutter

I am new in Firestore database.我是 Firestore 数据库的新手。 I am using an app using flutter and Firestore.我正在使用使用 flutter 和 Firestore 的应用程序。 The database structure for my app is like that:我的应用程序的数据库结构是这样的:

Rooms --Room1-----Reservations.....reservation dates
                              .....reservation dates
        Room2-----Reservations.....reservation dates
        Room3
        Room4-----Reservations.....reservation dates
        Room5-----Reservations.....reservation dates
                              .....reservation dates
                              .....reservation dates

'Rooms' is level 1 collection which holds all the room details. “房间”是包含所有房间细节的 1 级集合。 Each of the room data holds the 'Reservations' collection to holds all reservation details(check_in and check_out date) for that room.每个房间数据都包含“预订”集合以保存该房间的所有预订详细信息(入住和退房日期)。 Now I want to get the list of rooms which are available in a specific date span.现在我想获取在特定日期范围内可用的房间列表。 How to work with 'group collection query' for this requirement?如何使用“组集合查询”来满足此要求? Or it is possible to do the same by 'group collection query'?或者可以通过“组集合查询”来做同样的事情?

Late but, if anyone comes back to this, now in firestore there is something called collectionGroup query where you can get all subcollections if they have same name.晚了但是,如果有人回到这个,现在在firestore中有一个叫做collectionGroup查询的东西,如果它们具有相同的名称,您可以在其中获取所有子集合。 Which can be done as可以这样做

Firestore.instance.collectionGroup('Reservations').getDocuments()

This will give you the list of all the Reservations across all rooms.这将为您提供所有房间的所有预订列表。 Please read in detail about collection group query here in the documentation请在文档中详细阅读有关集合组查询的信息

There's no direct way to get documents of sub collection you can get documents from all collections.没有直接的方法来获取子集合的文档,您可以从所有 collections 获取文档。 Below is the code to perform multiple collection queries and save them to a list.下面是执行多个集合查询并将它们保存到列表的代码。

class SearchService {
Future<QuerySnapshot> getRoomsData() async {
return await Firestore.instance
    .collection('rooms')
    .getDocuments();
 }
}

Then get that snapshot and perform another query.然后获取该快照并执行另一个查询。 @override void initState() { _getSnapShots(); @override void initState() { _getSnapShots(); super.initState(); super.initState(); } }

  _getSnapShots() {
List<String> list = List<String>();
SearchService().getRoomsData().then((QuerySnapshot snapshot){
  snapshot.documents.map((DocumentSnapshot docs){
    docs.reference.collection('Reservations').getDocuments().then((QuerySnapshot snapshot){
      snapshot.documents.map((DocumentSnapshot docs){
        list.add(docs.data['reservation dates']);
      }).toList();
    });
  }).toList();
});
}

Now I want to get the list of rooms which are available in a specific date span.现在我想获取在特定日期范围内可用的房间列表。

Given that you store reservations, a room is available when there is no reservation for that time.鉴于您存储了预订,当当时没有预订时,一个房间是可用的。 Cloud Firestore cannot query for the absence of data, so in your current model that query won't be possible. Cloud Firestore 无法查询是否缺少数据,因此在您当前的 model 中无法进行该查询。

If you'd store the available slots in each room's Reservations collection, you could query across all Reservations collections to get the available slots across all rooms.如果您将可用插槽存储在每个房间的Reservations集合中,您可以查询所有Reservations collections 以获取所有房间的可用插槽。 But note that the result of this query are documents from Reservations , and not the individual rooms;但请注意,此查询的结果是来自Reservations的文档,而不是单个房间; you will need to load each room separately if you need information from that.如果您需要其中的信息,您将需要分别加载每个房间。

Alternatively, you could store a list of still-available slots in each Room document.或者,您可以在每个 Room 文档中存储仍然可用的插槽列表。 With that you could do a regular query across all rooms to see which ones are still available, and then perform an update across the Room document and its Reservations collection in a transaction.有了它,您可以对所有房间进行常规查询以查看哪些仍然可用,然后在事务中对 Room 文档及其Reservations集合执行更新。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM