[SOLVED] Sort on date column not working useMoralisQuery

Hi Moraliser,
I have some code fetch data from my custom Collection but I couldn’t sort .

const {data, fetch} = useMoralisQuery('Notify', (query) => query.descending('updatedAt'), [], {
    live: true,
  });

Not working with updateAt, createdAt.
My syntax incorrect?

Thanks for support me

Anyone got this issues ?

Sorry for the delay in answering this - your code looks fine, what do you mean by it doesn’t work? Does the data not get sorted properly or does no data show? Make sure to check the attributes for each object for the updatedAt value.

What is your serverUrl?

Hi Glad,

Data not get sorted for me

What is your serverUrl? Can you console.log the attributes of the first and last object in data?

Hi Glad,
My serverURL : https://bpda3zuzv4e4.usemoralis.com:2053/server

const {fetch} = useMoralisQuery(
    'Transactions',
    (query) => query.descending('createdAt').limit(25),
    [],
    {
      autoFetch: false,
    }
  );

My first object :

Last object:

Still not sort by createdAt or updatedAt :frowning:

Try using _created_at instead of createdAt while sorting. Seems like createdAt column is getting sorted as a string. You can do the same using _created_at.

I tried _created_at but not working too. I don’t know how to sort by Date

Looks like your Transactions class doesn’t order at all for createdAt if you do it manually in your server dashboard (by clicking createdAt column name).

Based on the objectIds, it looks like you have created these objects through MongoDB and not Parse - how did you do this? There may be something else you have to do. Otherwise you can just create those objects with Parse (or moralis-v1).

Hi Glad,

I write api connect direct to MongoDB and insert records into MongoDB.
I have system backend working with MongoDB
CreateAt not sort but UpdateAt worked.
But UpdateAt sort NOT by Date, I though it sort by ‘string’ on column UpdateAt

Any update @alex ??

Still waiting for your reply :slight_smile:

I am not sure what you need to do if you create the objects/records directly through MongoDB to be able to sort them through Parse.

I will look into it if you haven’t done so - the alternative is to create the objects normally through Parse or the moralis-v1 SDK.

Can you post your MongoDB insert code as well.

Hi Glad,

I sure createdAt and updatedAt not working on sort. I sort normally through dashboard Moralis still not working. Just click and click on head columns, icon triangle change up and down but data not sort.

I insert 2 more fields createdDate and updatedDate. And sort by this fields so good.
Thanks, resolved my issues