Home > Mobile >  Cloud Firestore - Document naming problem
Cloud Firestore - Document naming problem

Time:11-22

I'm having a hard time with implementing the »correct« Cloud Firestore document naming.

I have a web storage server with more than 5000 photos. Photos are named 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). 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:

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.

What worked:

  • Photos (collection)
  • Photo_1 (document)
  • Photo_comments (collection)
  • UUIDs (document)

// 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). How can I prevent document duplicates with the same field values?

CodePudding user response:

The simplest solution for this would be to use CollectionReference#add() method which:

Adds a new document to this collection with the specified data, assigning it a document ID automatically.

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«.

  • Related