Swift - Firebase - order collection

后端 未结 3 920
无人共我
无人共我 2020-12-22 05:47

I am trying to retrieve some documents but I need them to be ordered by some data (\"ListIDX\") inside my \"Wishlists\" - collection.

I tried this but t

相关标签:
3条回答
  • 2020-12-22 05:55

    As @algrid says, there is no sense to order the collection using order() if you are going to get an specific element using list.name at the end, not the first or the last. I would suggest to change your code to:

    db.collection("users").document(userID).collection("wishlists").document(list.name).collection("wünsche").getDocuments()
    
    0 讨论(0)
  • 2020-12-22 05:55

    I solved the problem. I added another attribute when saving a wish that tracks the index of the list it is being added to. Maybe not the smoothest way but it works. Thanks for all the help :)

    0 讨论(0)
  • 2020-12-22 06:08

    I am trying to retrieve some documents but I need them to be ordered by some data ("ListIDX")

    The following line of code will definitely help you achieve that:

    db.collection("users").document(userID).collection("wishlists").order(by: "ListIDX").getDocuments() {/* ... */}
    

    Adding another .document(list.name) call after .order(by: "ListIDX") is not allowed because this function returns a Firestore Query object and there is no way you can chain such a function since it does not exist in that class.

    Furthermore, Firestore queries are shallow, meaning that they only get items from the collection that the query is run against. There is no way to get documents from a top-level collection and a sub-collection in a single query. Firestore doesn't support queries across different collections in one go. A single query may only use the properties of documents in a single collection. So the most simple solution I can think of would be to use two different queries and merge the results client-side. The first one would be the above query which returns a list of "wishlists" and the second one would be a query that can help you get all wishes that exist within each wishlist object in wünsche subcollection.

    0 讨论(0)
提交回复
热议问题