Skip to content Skip to sidebar Skip to footer

How Do Query All Documents Of A Collection In Fire Store

I am trying to display information from Cloud Firestore on Google maps. I am able to apply a marker from a document using the below code, however I have multiple documents that I n

Solution 1:

To get the values of your latitude and longitude properties of all documents, simply use the following lines of code:

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference notesRef = rootRef.collection("notes");
notesRef.get().addOnCompleteListener(newOnCompleteListener<QuerySnapshot>() {
    @OverridepublicvoidonComplete(@NonNull Task<QuerySnapshot> task) {
        if (task.isSuccessful()) {
            for (QueryDocumentSnapshotdocument : task.getResult()) {
                float lat = Float.parseFloat(document.getString("Latitude"));
                float lng = Float.parseFloat(document.getString("Longitude"));
                String name = document.getString("Attraction Name");
                LatLng latLng = newLatLng(lat, lng);
                mMaps.addMarker(newMarkerOptions().position(latLng).title(name));
            }
        }
    }
});

So the key for solving this problem is the use of task.getResult() that can help you iterate through the QueryDocumentSnapshot object.

Solution 2:

If You want to all document data you can try this:-

db.collection("Chat").addSnapshotListener(new EventListener<QuerySnapshot>() {
            @Override
            public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {
                for (DocumentChange dc : queryDocumentSnapshots.getDocumentChanges()) {
                    switch (dc.getType()) {
                        caseADDED:
                            //  Log.d("TAG", "New Msg: " + dc.getDocument().toObject(Message.class));break;
                        caseMODIFIED:

                            //      Log.d("TAG", "Modified Msg: " + dc.getDocument().toObject(Message.class));break;
                        caseREMOVED:
                            //     Log.d("TAG", "Removed Msg: " + dc.getDocument().toObject(Message.class));break;
                    }
                }
            }
        });

Post a Comment for "How Do Query All Documents Of A Collection In Fire Store"