简体   繁体   中英

How to obtain data from collection in firestore with where clause?

I want to obtain data (langtitude field) from collection in firestore, with where clause, but it doesn't work.

My code:

db = FirebaseFirestore.getInstance();
Query query = db.collection(collection).whereGreaterThan("longtitude", String.valueOf(bounds.northeast.latitude));
query.addSnapshotListener(new EventListener<QuerySnapshot>() {
            @Override
            public void onEvent(@javax.annotation.Nullable QuerySnapshot queryDocumentSnapshots, @javax.annotation.Nullable FirebaseFirestoreException e) {
                if (e != null) {
                    Log.w(TAG, "Listen failed.", e);
                    return;
                }

                if (queryDocumentSnapshots != null) {
                    Log.d(TAG, queryDocumentSnapshots.getMetadata().toString());        
                } else {
                    Log.d(TAG, "Current data: null");
                }
            }
        });

I only get

SnapshotMetadata{hasPendingWrites=false, isFromCache=false}

But I want fields from collection. How to do it?

I tried different methods and none of them returned a fields in a collection.

When you run a query against a collection, you get a QuerySnapshot which can contain zero or more documents. To get the actual documents, you'll need to loop over the documents as shown here :

public void onEvent(@javax.annotation.Nullable QuerySnapshot queryDocumentSnapshots, @javax.annotation.Nullable FirebaseFirestoreException e) {
    if (e != null) {
        Log.w(TAG, "Listen failed.", e);
        return;
    }

    for (QueryDocumentSnapshot doc : queryDocumentSnapshots) {
        if (doc.get("longtitude") != null) {
            Log.d(TAG, doc.getString("longtitude"));
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM