[英]Cloud Firestore - Document naming problem
I'm having a hard time with implementing the »correct« Cloud Firestore document naming.我很难实施“正确”的 Cloud Firestore 文档命名。
I have a web storage server with more than 5000 photos.我有一台拥有 5000 多张照片的 web 存储服务器。 Photos are named 1.jpg -> 5000.jpg.照片命名为 1.jpg -> 5000.jpg。 I have decided to add a comment system for each photo.我决定为每张照片添加一个评论系统。
Example: The app will show a random photo from a server (eg. 123.jpg).示例:应用程序将显示来自服务器的随机照片(例如 123.jpg)。 Users will be able to comment on that photo and reply to other comments.用户将能够评论该照片并回复其他评论。
The only thing that currently worked is naming the document like this: »photo_1« -> »photo_5000«, but it's recommended not to use:当前唯一可行的方法是将文档命名为:»photo_1« -> »photo_5000«,但建议不要使用:
Do not use monotonically increasing document IDs such as: • Customer1, Customer2, Customer3, ... • Product 1, Product 2, Product 3, ... Such sequential IDs can lead to hotspots that impact latency.不要使用单调递增的文档 ID,例如: • Customer1、Customer2、Customer3, ... • Product 1, Product 2, Product 3, ... 这样的顺序 ID 可能会导致影响延迟的热点。
What worked:什么有效:
// Android // Android
int currentPhotoNumber = 13;
firebaseFirestore.collection("photos").whereEqualTo("photo_id", currentPhotoNumber).get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
QuerySnapshot snapshot = task.getResult();
if (!snapshot.isEmpty()) {
// Document with the photo_id of "currentPhotoNumber" already exists
} else {
// Document does not exist. We need to create one.
Map<String, Object> data = new HashMap<>();
data.put("photo_id", currentPhotoNumber);
// Create a new document named photo_13
firebaseFirestore.collection("photos").document("photo_" + currentPhotoNumber).set(data);
}
}
// Add a comment to photo_13
for (QueryDocumentSnapshot document : task.getResult()) {
DocumentReference documentReference = document.getReference();
documentReference.collection("photos").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
Map<String, Object> data = new HashMap<>();
data.put("author", "Name");
data.put("author_id", "1234");
data.put("comment", "This is a comment for photo 13");
data.put("time", FieldValue.serverTimestamp());
documentReference.collection("photo_comments").add(data);
}
});
}
}
});
It works if I create documents with UUID and set the field "photo_id" with currentPhotoNumber but it gets messy if a lot of users post comments at the same time if the photo_id is not set for particular photo as it creates multiple documents with the same fields and values (eg. photo_id = 13).如果我使用 UUID 创建文档并使用 currentPhotoNumber 设置字段“photo_id”,它会起作用,但是如果很多用户同时发表评论,如果 photo_id 没有为特定照片设置,它会变得混乱,因为它会创建多个具有相同字段的文档和值(例如 photo_id = 13)。 How can I prevent document duplicates with the same field values?如何防止具有相同字段值的文档重复?
The simplest solution for this would be to use CollectionReference#add() method which:最简单的解决方案是使用CollectionReference#add()方法,该方法:
Adds a new document to this collection with the specified data, assigning it a document ID automatically.使用指定的数据向此集合添加一个新文档,并自动为其分配一个文档 ID。
That being said, to identify a photo, simply add a field inside the document called imageName
that will hold the actual image names »photo_1« -> »photo_5000«
.话虽如此,要识别照片,只需在文档中添加一个名为imageName
的字段,该字段将保存实际的图像名称»photo_1« -> »photo_5000«
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.