简体   繁体   中英

How can I segregate user data in firebase?

I'm creating an app that has uses a firebase Cloud Firestore database. The structure seems to be collection/document/fields. I am thinking of either using the user id as the prefix to the collection name or simply a field for userId.

I'm currently using:

Firestore.firestore().collection("Events").  

This could be changed to prefix event with the userId

I am currently reading everything in using:

reference(to: "Events").addSnapshotListener{ (snapshot, _) in
    guard let snapshot = snapshot else {return}
    for document in snapshot.documents {
        // code here
    }
}

It's not a really good idea to use prefixes on collection names. That doesn't work well with security rules.

The usual structure for per-user data is to have a collection with documents whose IDs are the user IDs. Then, you can further organize other data in subcollections under that document ID.

/users
  /uid1
  /uid2
    /events
    /likes
    /history

Then, you can write security rules using the user's UID very easily.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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