Back

[SOLVED] How to make query to fetch only todays data in appwrite cloud version

  • 0
  • Flutter
conqueror
31 May, 2023, 22:00

How to make query to fetch only todays data in appwrite cloud version

TL;DR
Solution: To fetch only today's data in the Appwrite cloud version, you can use the following query: ```dart final now = DateTime.now().toUtc(); final todayStart = DateTime.utc(now.year, now.month, now.day); final res = await db.listDocuments( databaseId: '5439e7ae4abb114f2b1', collectionId: '843ee466b451ce50ad7', queries: [ Query.offset(0), // Start with an offset of 0 Query.orderDesc("\$createdAt"), Query.greaterThan("\$createdAt", todayStart
Drake
31 May, 2023, 23:43

you can use greater than and pass an iso formatted string in UTC timezone as the value

conqueror
1 Jun, 2023, 11:51

thanks

Drake
1 Jun, 2023, 13:06

[SOLVED] How to make query to fetch only todays data in appwrite cloud version

conqueror
2 Jun, 2023, 20:31

trying to fetch todays data but its not working as aspected

TypeScript
    final todayStart = DateTime.utc(now.year, now.month, now.day);
    //print( todayStart.toIso8601String());

    // Fetch the initial batch of transactions
    final res = await db.listDocuments(
      databaseId: '5439e7ae4abb114f2b1',
      collectionId: '843ee466b451ce50ad7',
      queries: [
     
        Query.offset(0), // Start with an offset of 0
        Query.orderDesc("\$createdAt"),
        Query.greaterThan("\$createdAt", todayStart.toIso8601String()),
      ],
    );```
Drake
2 Jun, 2023, 20:34

what exactly is "not as expected"?

conqueror
2 Jun, 2023, 20:34

its showing yesterdays data also

Drake
2 Jun, 2023, 20:34

how about if you conver those to UTC? is it still yesterday?

conqueror
2 Jun, 2023, 20:37

yes its showing yesterdays and todays mix data , tried without converting UTC but its not fetching any data from server

Drake
2 Jun, 2023, 20:42

would you please share the raw string value of the oldest document in the response?

conqueror
2 Jun, 2023, 20:48

{code: [8904275300006, 8901030875212], productName: [Hallelujah , yeshu teri hamad kara ], sellingPrice: [12, 12], quantity: [4, 4], totalQuantity: [], productUnit: [Piece, Piece], date: 2023-06-02T18:03:35.026+00:00, paymentMode: Cash, $id: 6479e19fb2fb54da205f, $createdAt: 2023-06-02T12:33:35.733+00:00, $updatedAt: 2023-06-02T12:33:35.733+00:00, $permissions: [read("user:647387d4e0aab0d237ae"), update("user:647387d4e0aab0d237ae"), delete("user:647387d4e0aab0d237ae")], $collectionId: 643ee466b6cf1ce50ad7, $databaseId: 6439e7ae4abb3e14f2b1}

Drake
2 Jun, 2023, 20:49

2023-06-02T12:33:35.733+00:00 that looks like current day 🧐

Drake
2 Jun, 2023, 20:50

i don't think this conversion is right

conqueror
2 Jun, 2023, 20:51

ok, btw today is 2023-06-03 for me

Drake
2 Jun, 2023, 20:53

try this:

TypeScript
    final now = DateTime.now();
    final today = DateTime(now.year, now.month, now.day);
    //print( todayStart.toIso8601String());

    // Fetch the initial batch of transactions
    final res = await db.listDocuments(
      databaseId: '5439e7ae4abb114f2b1',
      collectionId: '843ee466b451ce50ad7',
      queries: [
     
        Query.offset(0), // Start with an offset of 0
        Query.orderDesc("\$createdAt"),
        Query.greaterThan("\$createdAt", today.toUtc().toIso8601String()),
      ],
    );
Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more