Answer the question
In order to leave comments, you need to log in
How to set the order of the columns in the selection in mongo?
Hello!
From the client to the server I pass the columns that I want to get in the selection, for example ["CAMPANY", "SYMBOL", "LAST"], but mongo / mongoose can return them in any order after the select. How to make sure that the result is returned in exactly that order?
Thanks!
Answer the question
In order to leave comments, you need to log in
Mongo returns just objects, what order are we talking about? Give an example where it is critical to refer to the result in order, and not to the names of the fields. You can fix the order of the fields in a separate array, and thus process.
If I understand the question correctly, then it's not about mongo / mongoose. The fact is that there are no associative arrays in js itself. There are objects and they are not ordered (like in V8 they are sorted alphabetically, but the standard is silent about this). If you really need it, then you can split the resulting object from the base into an array of "one key - value" objects and order this array.
The order of keys in objects in JS is not guaranteed (it's easier to assume that it doesn't exist).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question