Sort on date column not working with aggregate


const pipeline= [{sort:{"updatedAt": -1}},{limit:1}]
const query = new Parse.Query("TokenBalance");
const result = await query.aggregate(pipeline);
console.log(result);

first object in result array

data in collection

update - works with the query.descending("updatedAt) but not with aggregations. Aggregations are crucial for complex queries, why its not working?

maybe you can try to convert it to string and then use it in aggregation, not sure if it will work

but why customers cant sort based on date using aggregations?

I don’t know why that happens

There are threads on this forum mentioning this issue almost over a year old. Arent these bugs reported to the accordng department?

I didn’t know about this problem.

In the past there were some problems on sorting by date if there were different date formats in db in that column.

can this be investigated or forwarded? Feel like there are a good amount of issues from older posts still relevant now but new features get implemented almost monthly.

I’ll have to test it, to see that it replicates, then it can be forwarded

1 Like

thank you, lmk please so I can move on

it looks like this syntax works:


Moralis.Cloud.define('test_aggregate', async (request) => {
   const pipeline= [{sort:{"_updated_at": -1}},{limit:1}]
   const query = new Parse.Query("EthNFTTransfers");
   const result = await query.aggregate(pipeline);
  
   return result;
})

I ran into this issue a long time ago, and still using block_number as workaround instead of sorting on createdAt.
Referenced here Can't get sort to work in aggregate query
I tested out using _created_at instead and that works fine. thanks for that clarification @cryptokid!

1 Like

yes I switched to a new column timestamp with Date.now() values im using now cause the fields still seem bugged and its confusing if they are named differently using other tools than the browser dashboard.