简体   繁体   English

在 Firebase Cloud Firestore 中构建数据

[英]Structuring Data in Firebase Cloud Firestore

I had been trying to structure my database in firestore我一直在尝试在 Firestore 中构建我的数据库

my json looks like this我的 json 看起来像这样

{"UID":"myusrsid","PhotoList":[{"PhotoID":333,"PhotoURL":"url1ofphone"}, {"PhotoID":332,"PhotoURL":"url2ofphoto"} ]}

Here UID is UserID and its unique to users and this UID inside this have Photos List which have photo id and its url这里的 UID 是用户 ID,它对用户是唯一的,其中的这个 UID 有照片列表,其中有照片 ID 和它的 url

so whenever user upload a new photo if the userid already exist the photo should be added to Photos List and if new user then UID with new user should be created and photo should be added in photo list因此,每当用户上传新照片时,如果用户 ID 已存在,则应将照片添加到照片列表中,如果是新用户,则应创建新用户的 UID,并将照片添加到照片列表中

I had been scratching my head to structure this我一直在挠头来构建这个

 public class Users
{
    public string UID { get; set; }
    public List<PhotoList> PhotoList { get; set; }
}

public class PhotoList
{
    public int PhotoID { get; set; }
    public string PhotoURL { get; set; }
}

this is my model which I am planing to send and receive data这是我打算发送和接收数据的 model

I am aware of retrieving and storing the data the only thing is right now I need help with how should I structure it.我知道检索和存储数据,唯一的事情是现在我需要帮助我应该如何构建它。 I am using Node js我正在使用节点 js

here is what I had been doing right now这就是我现在一直在做的事情

const imageBody = {
            uid,
            imageBase64String,
            
        } = req.body;

        await db.collection('users').doc(imageBody.uid).collection('Photos').doc(imageBody.imageBase64String).create({
            url : imageBody.imageBase64String
        });

Firestore has a maximum document size, so storing binary files is not recommended. Firestore 具有最大文档大小,因此不建议存储二进制文件。 Rather use Firebase Storage and store the URL in Firestore.而是使用 Firebase 存储并将 URL 存储在 Firestore 中。

If you still want to store the images in Firestore consider using a flatter structure.如果您仍想将图像存储在 Firestore 中,请考虑使用更扁平的结构。 You can still maintain strong access control and it will also be easier for other users share photos.您仍然可以保持强大的访问控制,并且其他用户共享照片也会更容易。 By putting access control in the document itself you can get really powerful sharing capability without sacrificing security.通过将访问控制放在文档本身中,您可以获得非常强大的共享功能,而不会牺牲安全性。 So something like:所以像:

Users

Photos
  ownerUID
  imageBase64String
  readPrivs

And to access images for a user:并为用户访问图像:

await db.collection("Photos").where("ownerUID", "==", uid).get();

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

相关问题 本地云功能Firebase将数据添加到Firestore - Local Cloud Function Firebase add data to Firestore Firebase Cloud Functions-在Firestore中移动数据 - Firebase Cloud Functions - move data in Firestore 从云中的 Firebase Firestore 获取数据 Function - Get Data from Firebase Firestore in Cloud Function 使用 firebase 云 function 从云 Firestore 读取数据? - Read data from cloud firestore with firebase cloud function? 使用 Firebase 云从 Cloud Firestore 读取数据 function - Read data from Cloud firestore with Firebase cloud function Firebase Cloud Function:Cloud Firestore 查询无效,即使数据在 Cloud Firestore 中 - Firebase Cloud Function : Cloud Firestore query invalid eventhough data is in Cloud Firestore Firebase云功能/ Firestore - Firebase Cloud Functions / Firestore 用于导出 Firestore 备份数据的云功能。 使用 firebase-admin 还是 @google-cloud/firestore? - Cloud function to export Firestore backup data. Using firebase-admin or @google-cloud/firestore? Firebase:Firestore 未更新 Cloud Function 的计划函数中的数据 - Firebase : Firestore not updating data in Cloud Function's scheduled function 数据未通过调度程序 function 和 Firebase Cloud Functions 保存到 Firestore - Data not saved into Firestore through scheduler function with Firebase Cloud Functions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM