K
K
karmyskove2021-05-16 11:45:08
Node.js
karmyskove, 2021-05-16 11:45:08

How to sort collections?

Hello everyone, I need to sort the collection, for this I wrote this code:

const test= await Test.find({}).sort({id: 'desc', date: 'asc'});

Everything works fine, I decided to make the sorting dynamically changeable, changed the route, changed the code: The problem is this, I can only pass the parameter by which it will be sorted to the sort property, I can’t select the sort type, when I write this code, idea gives an error:
const test= await Test.find({}).sort(sortField);
const test= await Test.find({}).sort(sortField: sortType);
</code
Как можно решить эту проблему?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Yarkov, 2021-05-16
@karmyskove

[sortField]: sortType

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question