NightKnight
NightKnight

Reputation: 38

How to get document id from firestore database

I'm trying to retrieve a document id by calling doc.getId() but I can't call it from this code

        db.collection("Pegawai").addSnapshotListener(new EventListener<QuerySnapshot>() {
            @Override
            public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {
                if(e != null) {
                    Log.d(TAG, e.toString());
                }
                if (queryDocumentSnapshots != null) {
                    for (DocumentChange doc: queryDocumentSnapshots.getDocumentChanges()) {
                        if (doc.getType() == DocumentChange.Type.ADDED) {
                            Pegawai pegawai = doc.getDocument().toObject(Pegawai.class);
                            pegawai.setId(doc.getId());
                            pegawaiList.add(pegawai);

                            pegawaiListAdapter.notifyDataSetChanged();
                        }
                    }
                }
            }
        });

I've tried this code, and apparently, I can call doc.getId() with this code, but this code isn't populating my recyclerview at all

        db.collection("Pegawai").addSnapshotListener(new EventListener<QuerySnapshot>() {
            @Override
            public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {
                if(e != null) {
                    Log.d(TAG, e.toString());
                }
                if (queryDocumentSnapshots != null) {
                    for (DocumentSnapshot doc : queryDocumentSnapshots) {
                        Pegawai pegawai = doc.toObject(Pegawai.class);
                        pegawai.setId(doc.getId());
                        pegawaiList.add(pegawai);
                    }
                }
            }
        });

Upvotes: 1

Views: 12134

Answers (3)

Ally Makongo
Ally Makongo

Reputation: 269

If you are using FirestoreRecyclerAdapter with Recyclerview do this.

 DocumentSnapshot snapshot = options.getSnapshots().getSnapshot(position);
            String dbKey = snapshot.getId();
            Log.d(TAG, "The database Key is : "+ dbKey);

Then you can bind it on view holder like this

    holder.setDocumentId(dbKey);

Then you can you retrieve it anywhere, depending on your getters and setters. I hope this will help someone out there.

Upvotes: 2

Zakariah Siyaji
Zakariah Siyaji

Reputation: 1119

The following is how to solve it in Javascript, maybe this will help you to convert it to Java. Using the following method, we can retrieve the document id, dynamically. Notice that we use snapshotChanges(). This is important because once we subscribe to this function, contained in the subscription is the document id.

saveMyValuesHere: any[] = []

getPegawai() {

    return this.db.collection('Pegawai').snapshotChanges();

  }

 getPegawai().subscribe(serverItems => {
      this.saveMyValuesHere = [];
      serverItems.forEach(a => {
        const data = a.payload.doc.data();
        const id = a.payload.doc.id;
        this.saveMyValuesHere.push({ id, ...data});
      });
    });

Upvotes: -1

Doug Stevenson
Doug Stevenson

Reputation: 317372

A DocumentChange object has a method getDocument() which returns a QueryDocumentSnapshot object. This is a subclass of DocumentSnapshot which means it also has a getId() method. So you should be able to use doc.getDocument().getId() to get the ID of the document being processed.

Upvotes: 1

Related Questions