0

I have a database like this Shop > warehouse > categories > cat1,cat2,.. and every category has a name and its documents. I just want only the categories, and I tried with this code:

firestore.collection("shop/warehouse").addSnapshotListener(new EventListener<QuerySnapshot>() {
        @Override
        public void onEvent(@javax.annotation.Nullable QuerySnapshot queryDocumentSnapshots, @javax.annotation.Nullable FirebaseFirestoreException e) {
            if(queryDocumentSnapshots != null) {
                for (DocumentSnapshot doc : queryDocumentSnapshots) {
                    String item = doc.getId();
                    Log.d("TEST ITEM", item);
                }
            } else Log.d(TAG, "queryDocumentSnapshots is null.");
        }
    });

But as expected I got this error:

Invalid collection reference. Collection references must have an odd number of segments, but shop/warehouse has 2

So I googled and found a solution that suggests to divide in collection/document and I wrote this:

firestore.collection("shop/").document("warehouse").addSnapshotListener(new EventListener<DocumentSnapshot>() {
        @Override
        public void onEvent(@javax.annotation.Nullable DocumentSnapshot documentSnapshot, @javax.annotation.Nullable FirebaseFirestoreException e) {
            Map<String, Object> map = documentSnapshot.getData();
            Log.d("size", map.keySet().size()+"");
        }

But this keyset (I assumed they're the category names) is empty. How can I do it? Have mercy on me, it's my first time with Firebase!

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Sabatino
  • 123
  • 3
  • 16
  • Possible duplicate of [Firebase Cloud Firestore : Invalid collection reference. Collection references must have an odd number of segments](https://stackoverflow.com/questions/46639058/firebase-cloud-firestore-invalid-collection-reference-collection-references-m) – Gastón Saillén May 29 '18 at 00:05
  • @GastónSaillén it's not a duplicate, I've tried the solutions below that question and they didn't work – Sabatino May 29 '18 at 06:32

2 Answers2

0

To solve this, please change the following line of code:

firestore.collection("shop/").document("warehouse").addSnapshotListener(/* ... */);

to

firestore.collection("shop").document("warehouse").addSnapshotListener(/* ... */);

There is no need for that /. You can use a reference like this, "shop/warehouse" only in Firebase Realtime database where all objects all called childs and you can chain all childs in a single path. It's a more elegant way but unfortunately is not permited in Cloud Firestore.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • You have this error `Invalid collection reference. Collection references must have an odd number of segments, but shop/warehouse has 2` when using `firestore.collection("shop").document("warehouse")`? – Alex Mamo May 29 '18 at 18:57
  • Is there everything alright, have you solved the issue? – Alex Mamo May 30 '18 at 04:31
  • No, I still get this error sadly, removing '\' nothing changes – Sabatino May 30 '18 at 07:33
  • Can you please add your database structure. – Alex Mamo May 30 '18 at 07:45
  • Please add a more detailed database structure to include also the documents within your categories. – Alex Mamo May 30 '18 at 10:24
  • Please also add the content of one document. What is beneath one last document. – Alex Mamo May 30 '18 at 11:07
  • 1
    Seeing your entire database structure, I can say that you cannot get only those ids witout quering the databse. As Doug said, you need to query the entire database to read all those documents in the collection in order to get only those ids. – Alex Mamo May 30 '18 at 13:24
0

There's no API to just list out the document IDs that exist in a collection. You will have to query and read all of the documents in the collection just to get the IDs.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • I don't know what you mean by "brute force", but as I said in my answer, you will have to query the entire collection, read all the documents, and get all their IDs. – Doug Stevenson May 30 '18 at 07:52
  • Excuse me, I meant doing it by hardcoding because when I query the document I get no data, here an example (sorry for the very low quality, I don't know why is it so poor) https://imgur.com/a/gNcxG5Y – Sabatino May 30 '18 at 10:54
  • Whatever it is you're doing there looks unrelated to the original question, – Doug Stevenson May 30 '18 at 12:27