0

I have recently started learning Swift and Firestore and i have a question around how to structure a database for what im doing.

At the moment i have the following structure. enter image description here

What i would like to do is to fetch the newest collection with a specific ownerID in the above structure am i right in suggesting i would have to fetch all documents and add it to an array and go from there, or would it work better with a structure where the document's id is the ownerID. I feel like fetching all the documents is a real waste and would become really problematic if there were lots of documents.

It maybe obvious that im a little lost here. Thanks in advance Luke

At the moment i have fetched the documents and sorted them from newest to oldest but now that i need the newest one with a specific ownerID i feel thats its a really clumsy way to do it.

import Foundation import FirebaseFirestore import FirebaseFirestoreSwift

class ThingManager: ObservableObject {

@Published private(set) var things: [Things] = []
@Published private(set) var lastThingId = ""
@Published var firstElement = ""

let db = Firestore.firestore()

init(){
    getThings()
}


func getThings() {
    db.collection("Things").addSnapshotListener { querySnapshot, error in
        guard let documents = querySnapshot?.documents else {
            print("Error: fetching documents \(String(describing: error))")
            return
        }
        
        self.things = documents.compactMap{ document -> Things? in
            do {
                return try document.data(as: Things.self)
            } catch {
                print("Error: decoding document into Thing failed: \(error)")
                return nil
            }
        }
        self.things.sort { $0.timestamp > $1.timestamp }
        
        if let Element = self.things.first?.thing {
            self.firstElement = Element
            print("First Element : \(self.firstElement)")
        }
        if let id = self.things.last?.id {
            self.lastThingId = id
        }
    }
}
jnpdx
  • 45,847
  • 6
  • 64
  • 94
  • In the NoSQL world, we are usually structuring a database according to the queries that we want to perform. What are those queries? – Alex Mamo Nov 27 '22 at 12:41
  • So i would like to fetch collections based on the user that created them, at the moment i have the ownerID of the user that created it in the collection its self. What im wondering is if it is better to make the document ID the ownerID so i can just fetch all documents with the ID of ownerID – LukeHahn Nov 28 '22 at 00:13
  • Why not? You can definitely do that. Does that help with your queries? If yes, then you can go ahead with it. – Alex Mamo Nov 28 '22 at 07:52
  • Ok so what your saying is that there is no right or wrong way to structure your data in Firestore its all about what you are going to want to do with it. p.s thanks for the reply it has actually clear alot up for me – LukeHahn Nov 29 '22 at 04:06
  • Yes, [there is no "perfect", "the best" or "the correct" solution for structuring a Cloud Firestore database](https://stackoverflow.com/a/53057707/5246885). – Alex Mamo Nov 29 '22 at 04:40

0 Answers0