New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How is sort order maintained when using the object notation? #4941
Comments
Yep, that's an unfortunate dilemma for both mongoose and the mongodb driver. ECMAScript says that keys are not ordered, but they are actually ordered in V8 (except for numeric keys, which is a nasty edge case), and likely will continue to be ordered in V8 for the foreseeable future. You can use the |
Thanks for clarifying and adding support for Maps. |
I tried to use this syntax but I got this error:
|
@aguyinmontreal what version of mongoose and mongodb? |
@vkarpov15 Mongoose 4.7.4 MongoDB 3.4.1 |
Sort using Map fails for me (mongoose v 4.10.5, mongodb 3.4.10). Here's what I've tried: And the error I get:
|
@tonybranfort you don't need to use Map, just do |
Thanks vkarpov15. I don't actually create the Map for the sort. It already exists before the sort - thought I could sort without converting it out of a Map. No problem to convert it before the sort. And thanks for all your work on Mongoose. |
Still getting the "the $sort key specification must be an object" error when running:
Mongoose 5.3.1. |
@NikitaVlaznev I'm not certain MongoDB aggregation supports that syntax, but I'll double check |
Yeah unfortunately the MongoDB server does not support the array-based syntax. However, you don't actually need this syntax since the ES6 spec specifies the order of keys in an object, so any spec-compliant JS runtime like node is going to give you the right key order every time. |
ECMAScript defines an object as follows:
So when using the
sort
method with the object notation (e.g.query.sort({ field: 'asc', test: -1 });
), how can the order of the keys actually be maintained? Currently, V8 does maintain the order, but can we rely on that to never change?The official Node MongoDB driver actually suggests to use different notations for whether there is one or several sort properties:
The text was updated successfully, but these errors were encountered: