ltonetwork / mongodb-rest

JSON REST server for MongoDB (using node.js)
MIT License
405 stars 143 forks source link

Reverse sort order #23

Closed Filirom1 closed 6 years ago

Filirom1 commented 12 years ago

In the following exemple, I try to sort the result with a desc order

$ curl -d '{ "A1" : 201 }' -H "Content-Type: application/json" http://localhost:3000/test/example1
$ curl -d '{ "A1" : 3 }' -H "Content-Type: application/json" http://localhost:3000/test/example1
$ curl -d '{ "A1" : 307 }' -H "Content-Type: application/json" http://localhost:3000/test/example1

Sort asc (easy) :

$ curl http://localhost:3000/test/example1?sort=A1

[
  {"A1":3,"_id":"4ed3aee8cc4fc51e1a000002"},
  {"A1":201,"_id":"4ed3aedccc4fc51e1a000001"},
  {"A1":307,"_id":"4ed3aeedcc4fc51e1a000003"}
]

But how can I do a desc sort:

I tried with:

> encodeURI("{A1:-1}")
'%7BA1:-1%7D'

$ curl http://localhost:3000/test/example1?sort=%7BA1:-1%7D

[
  {"A1":201,"_id":"4ed3aedccc4fc51e1a000001"},
  {"A1":3,"_id":"4ed3aee8cc4fc51e1a000002"},
  {"A1":307,"_id":"4ed3aeedcc4fc51e1a000003"}
]

But it doesn't work.

alexeimoisseev commented 12 years ago

Possibly this commit can fix the issue: https://github.com/tdegrunt/mongodb-rest/pull/25

Minstel commented 6 years ago

This can be done with ?sort={"A1":1} for ASC order and ?sort={"A1":-1} for DESC order.