[SOLVED] Can't retrieve data based on dates :(

Moralis.Cloud.define("returnData", async (request) => {
  const query = new Moralis.Query("PolygonTransactions");
  let start=new Date(2022,03,28);
  let end=new Date(2022,04,23);
  const pipe=[
                {
                  match:{
                    $expr:{
                      $and:[
                        {$gte:["$createdAt",start]},
                        {$lte:["$createdAt",end]}
                      ]
                    }
                  }
                }
              ];
  const data=await query.aggregate(pipe);
  return data;
  });

The function returns [] although there exists data. I need to resolve this issue ASAP.

You have to convert the date to iso string

An example:

                    {$gte: ["$block_timestamp", {"$toDate": new Date('2022-05-01').toISOString()}]},


For example:
I need to fetch the transactions from start to end date given by the user from the frontend.

Moralis.Cloud.define("returnData", async (request) => {
  const query = new Moralis.Query("PolygonTransactions");
  let start=new Date(2022,02,01).toISOString();
  //start.setHours(start.getHours()+4);
  let end=new Date(2022,05,23).toISOString();
 // end.setHours(end.getHours()+4);
  const pipe=[
                {
                  match:{
                    $expr:{
                      $and:[
                        {$gte:["$createdAt",{"$toDate":start}]},
                        {$lte:["$createdAt",{"$toDate":end}]}
                      ]
                    }
                  }
                }
              ];
  const data=await query.aggregate(pipe);
  return data;
  });

But this code always returns []. Please help me am in the verge of completing my final assignment.

try to use some logging to see what values you have for start and end

also, you may have to use _created_at instead of $createdAt

Still returns the same - [] empty array. :frowning:

What is the code that you use now?
Try to remove/comment parts of that condition with date limits
Did you log the date value?

Start and end date

Try to also add master key parameter

Try without date filters to see if it works

Yes it works without any filters. But this isn’t I was looking for :frowning:

Ok, now add knelt one filter. Try various ways for that filter and date.

Also, Try to post current code that you use.

Above I have pasted the code below the PolygonTransactions collections image :frowning:

Yes, that is the original code, that code is not going to work. What is the updated code that you use now?

Moralis.Cloud.define("returnData", async (request) => {
  const query = new Moralis.Query("PolygonTransactions");
  let start=new Date("2022-01-01");
  
  let end=new Date("2022-05-30");
  const pipe=[
                {
                  match:{
                    $expr:{
                      $and:[
                        {$gte:["createdAt",start]},
                        {$lte:["createdAt",end]}
                      ]
                    }
                  }
                }
              ];
  const data=await query.aggregate(pipe,{ useMasterKey: true });
  return {data, start, end};
  });
  1. try to use _created_at instead of createdAt
  2. try to use something like new Date(β€˜2022-01-01’).toISOString()
Moralis.Cloud.define("returnData", async (request) => {
  const query = new Moralis.Query("PolygonTransactions");
  let start=new Date("2022-01-01").toISOString();
  
  let end=new Date("2022-05-30").toISOString();
  const pipe=[
                {
                  match:{
                    $expr:{
                      $and:[
                        {$gte:["_created_at",start]},
                        {$lte:["_created_at",end]}
                      ]
                    }
                  }
                }
              ];
  const data=await query.aggregate(pipe,{ useMasterKey: true });
  return {data,start,end};
  });

Updated code but with the same output. [] Array

it looks like this worked for me:

Moralis.Cloud.define("returnData", async (request) => {
  const query = new Moralis.Query("BscTokenTransfers");
  let start=new Date('2022-05-01').toISOString()
  let end=new Date('2022-06-01').toISOString()
  const pipe=[
                {
                  match:{
                    $expr:{
                      $and:[
                        {$gte: ["$_created_at", {"$toDate": start}]},
                        {$lte: ["$_created_at", {"$toDate": end}]}
                      ]
                    }
                  }
                }
              ];
  const data=await query.aggregate(pipe,{ useMasterKey: true });
  return {data,start,end};
  });
1 Like

It worked thanks a lotttt :laughing:

1 Like