2

I want to query Cloud Firestore data in last 7 days. I am using server timestamp of Firestore also storing the time in millis.

CollectionReference IOTransactions = db.collection(userID)
                .document("userData").collection("StockTransactions");

        Query transactionsQuery = IOTransactions.orderBy("timestamp",Query.Direction.DESCENDING);

        transactionsQuery.addSnapshotListener(new EventListener<QuerySnapshot>() {
            @Override
            public void onEvent(@javax.annotation.Nullable QuerySnapshot queryDocumentSnapshots, @javax.annotation.Nullable FirebaseFirestoreException e) {
                if (e != null) {
                    Log.w("FirestoreDemo", "Listen failed.", e);
                    return;
                }
                for (QueryDocumentSnapshot doc : queryDocumentSnapshots) {
                    if (doc.get("timestamp") != null)
                        transactionsList.add(doc.toObject(StockTransaction.class));
                    Log.d("Firestore", "Added item");
                }

                if(transactionsList.size() < 1 )
                    emptyTransactionsPage.setVisibility(View.VISIBLE);

                Log.d("Firestore Reading", "Successfully fetched");
                adapter.notifyDataSetChanged();
                pb.setVisibility(View.INVISIBLE);

            }
        });

How can I query the data created in last 7 days?

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
ertuzun
  • 153
  • 4
  • 15

2 Answers2

2

I am using server timestamp of Firestore also storing the time in milliseconds.

So if you are storing the timestamp in milliseconds, you are doing it wrong.

How can I query the data created in last 7 days.

To be able to query your database according to a periode of time, your timestamp property should be o type Date and not long. To see how you can achieve this, please take a look at my answer from this post.

Once you have the property set correctly, a query that looks like this should do the trick:

IOTransactions.whereLessThan("timestamp", now).whereGreaterThan("timestamp", sevenDayAgo);

In which now and sevenDayAgo are two Date objects representing the present time and a moment in time seven days ago.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • I guess I couldn't tell right that servertimestamp is in date format but I also store as millis in another field. Thank you for your answer it works for my situation. I have tried before with little differencies so it didn't work but this works well. – ertuzun Feb 16 '19 at 21:26
  • Alex, can you help me here ? https://stackoverflow.com/questions/61124994/how-to-fetch-documents-from-one-month-old-only-firestore/61125159#61125159 – SNM Apr 09 '20 at 18:22
  • @Alex Mamo Can you take a look at this https://stackoverflow.com/questions/62927499/how-to-use-pagination-to-firestore-query-using-timestamp – Shiva s Jul 16 '20 at 13:23
0

According to My Knowledge you can also use the following query to get the result

Query query = mdb.collection("$YOUR_COLLECTION_NAME").orderBy("$YOUR_FIELD")
               .whereGreaterThan("$YOUR_FIELD",$Begin_Date_Object)
               .endAt($YOUR_DATE_OBJECT);

Note: You need to declare a Java.Util.Date object to endAt field and orderBy must be always before startAt() or endAt() method.

The end at searches all documents containing the end at value of field in orderBy

Naveen S
  • 11
  • 5