Skip to content
This repository has been archived by the owner on Dec 10, 2018. It is now read-only.

Commit

Permalink
added sort
Browse files Browse the repository at this point in the history
  • Loading branch information
Kristina Chodorow committed Mar 12, 2010
1 parent af99db6 commit ef36da5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
7 changes: 5 additions & 2 deletions README.md
Expand Up @@ -57,19 +57,22 @@ Optional arguments:

* `criteria=search_criteria` (object)
* `fields=fields_to_return` (object)
* `sort=sort_fields` (object)
* `skip=num` (number)
* `limit=num` (number)
* `batch_size=num_to_return` (number)

Returns: `{"ok" : 1, "results" : [{...}, ... ], "id" : cursor_id}`

Example:
Examples:

Find all documents in the foo.bar namespace.

curl -X GET 'http://localhost:27080/foo/bar/_find'

TODO: sort
Find all documents in the foo.bar namespace and sort by x descending

curl -X GET 'http://localhost:27080/foo/bar/_find?sort=%7B%22x%22%3A-1%7D'

#### Get More Results

Expand Down
25 changes: 23 additions & 2 deletions handlers.py
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from pymongo import Connection, json_util
from pymongo import Connection, json_util, ASCENDING, DESCENDING
from pymongo.son import SON
from pymongo.errors import ConnectionFailure, OperationFailure

Expand Down Expand Up @@ -65,9 +65,13 @@ def _get_son(self, str, out):
try:
obj = json.loads(str)
except ValueError, TypeError:
out('{"ok" : 0, "errmsg" : "couldn\'t parse json: %s"' % str)
out('{"ok" : 0, "errmsg" : "couldn\'t parse json: %s"}' % str)
return None

if getattr(obj, '__iter__', False) == False:
out('{"ok" : 0, "errmsg" : "type is not iterable: %s"}' % str)
return None

# can't deserialize to an ordered dict!
if '$pyhint' in obj:
temp = SON()
Expand Down Expand Up @@ -191,6 +195,23 @@ def _find(self, db, collection, args, out):

cursor = conn[db][collection].find(spec=criteria, fields=fields, limit=limit, skip=skip)

sort = None
if 'sort' in args:
sort = self._get_son(args['sort'][0], out)
if sort == None:
return

stupid_sort = []

for field in sort:
if sort[field] == -1:
stupid_sort.append([field, DESCENDING])
else:
stupid_sort.append([field, ASCENDING])

cursor.sort(stupid_sort)


if not hasattr(self, "cursors"):
setattr(self, "cursors", {})

Expand Down

0 comments on commit ef36da5

Please sign in to comment.