简体   繁体   中英

FireStore How can i get Collection in an order (Dess / Accen )

i have a collecntion Which Contain Multiple document and i want to fetch higest index document form it

数据流 1

流程 2

流程 3

I want to fetch Lat document of Customer_Info but i dont know it value

and i dont know how to implement orderBy in collections => Document => document_Data

If Bill_ID is same as the document ID, you can use orderBy() and limit(1) to get last document in that collection as shown below:

db.collection("(?)/Business/Customer_Info")
        .orderBy("Bill_ID", Direction.DESCENDING)
        .limit(1)
        .get()
        .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if (task.isSuccessful()) {
                    for (QueryDocumentSnapshot document : task.getResult()) {
                        Log.d(TAG, document.getId() + " => " + document.getData());
                    }
                } else {
                    Log.d(TAG, "Error getting documents: ", task.getException());
                }
            }
        });

However, you must ensure the field must be a number or the order will not be correct as Firestore orders them lexicographically.


You should avoid using sequential documents IDs and using a timestamp to order documents might be a better idea.

Also checkout:

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