Skip to content

Commit 3411bde

Browse files
author
Mike Dirolf
committed
add sort parameter for find
1 parent 7688d58 commit 3411bde

File tree

4 files changed

+39
-8
lines changed

4 files changed

+39
-8
lines changed

doc/api/pymongo/cursor.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
.. automodule:: pymongo.cursor
55
:synopsis: Tools for iterating over MongoDB query results
66

7-
.. autoclass:: pymongo.cursor.Cursor(collection, spec, fields, skip, limit, slave_okay, timeout, tailable[, snapshot=False])
7+
.. autoclass:: pymongo.cursor.Cursor(collection, spec, fields, skip, limit, slave_okay, timeout, tailable[, snapshot=False[, sort=None]])
88
:members:
99

1010
.. describe:: c[index]

pymongo/collection.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ def _fields_list_to_dict(self, fields):
422422
return as_dict
423423

424424
def find(self, spec=None, fields=None, skip=0, limit=0,
425-
timeout=True, snapshot=False, tailable=False,
425+
timeout=True, snapshot=False, tailable=False, sort=None,
426426
_sock=None, _must_use_master=False, _is_command=False):
427427
"""Query the database.
428428
@@ -473,6 +473,12 @@ def find(self, spec=None, fields=None, skip=0, limit=0,
473473
continue from the last document received. For details, see
474474
the `tailable cursor documentation
475475
<http://www.mongodb.org/display/DOCS/Tailable+Cursors>`_.
476+
- `sort` (optional): a list of (key, direction) pairs
477+
specifying the sort order for this query. See
478+
:meth:`~pymongo.cursor.Cursor.sort` for details.
479+
480+
.. versionadded:: 1.6+
481+
The `sort` parameter.
476482
477483
.. versionchanged:: 1.6+
478484
The `fields` parameter can now be a dict or any iterable in
@@ -484,7 +490,7 @@ def find(self, spec=None, fields=None, skip=0, limit=0,
484490
.. mongodoc:: find
485491
"""
486492
if spec is None:
487-
spec = SON()
493+
spec = {}
488494

489495
slave_okay = self.__database.connection.slave_okay
490496

@@ -508,7 +514,7 @@ def find(self, spec=None, fields=None, skip=0, limit=0,
508514
fields = self._fields_list_to_dict(fields)
509515

510516
return Cursor(self, spec, fields, skip, limit, slave_okay, timeout,
511-
tailable, snapshot, _sock=_sock,
517+
tailable, snapshot, sort, _sock=_sock,
512518
_must_use_master=_must_use_master,
513519
_is_command=_is_command)
514520

pymongo/cursor.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,17 @@ class Cursor(object):
3737
"""
3838

3939
def __init__(self, collection, spec, fields, skip, limit, slave_okay,
40-
timeout, tailable, snapshot=False,
40+
timeout, tailable, snapshot=False, sort=None,
4141
_sock=None, _must_use_master=False, _is_command=False):
4242
"""Create a new cursor.
4343
44-
Should not be called directly by application developers.
44+
Should not be called directly by application developers - see
45+
:meth:`~pymongo.collection.Collection.find` instead.
4546
4647
.. mongodoc:: cursors
4748
"""
49+
self.__id = None
50+
4851
self.__collection = collection
4952
self.__spec = spec
5053
self.__fields = fields
@@ -54,15 +57,14 @@ def __init__(self, collection, spec, fields, skip, limit, slave_okay,
5457
self.__timeout = timeout
5558
self.__tailable = tailable
5659
self.__snapshot = snapshot
57-
self.__ordering = None
60+
self.__ordering = sort and helpers._index_document(sort) or None
5861
self.__explain = False
5962
self.__hint = None
6063
self.__socket = _sock
6164
self.__must_use_master = _must_use_master
6265
self.__is_command = _is_command
6366

6467
self.__data = []
65-
self.__id = None
6668
self.__connection_id = None
6769
self.__retrieved = 0
6870
self.__killed = False

test/test_collection.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,29 @@ def test_find_one_with_find_args(self):
869869
self.assertEqual(1, db.test.find_one()["x"])
870870
self.assertEqual(2, db.test.find_one(skip=1, limit=2)["x"])
871871

872+
def test_find_with_sort(self):
873+
db = self.db
874+
db.drop_collection("test")
875+
876+
db.test.save({"x": 2})
877+
db.test.save({"x": 1})
878+
db.test.save({"x": 3})
879+
880+
self.assertEqual(2, db.test.find_one()["x"])
881+
self.assertEqual(1, db.test.find_one(sort=[("x", 1)])["x"])
882+
self.assertEqual(3, db.test.find_one(sort=[("x", -1)])["x"])
883+
884+
def to_list(foo):
885+
return [bar["x"] for bar in foo]
886+
887+
self.assertEqual([2,1,3], to_list(db.test.find()))
888+
self.assertEqual([1,2,3], to_list(db.test.find(sort=[("x", 1)])))
889+
self.assertEqual([3,2,1], to_list(db.test.find(sort=[("x", -1)])))
890+
891+
self.assertRaises(TypeError, db.test.find, sort=5)
892+
self.assertRaises(TypeError, db.test.find, sort="hello")
893+
self.assertRaises(ValueError, db.test.find, sort=["hello", 1])
894+
872895
def test_insert_adds_id(self):
873896
doc = {"hello": "world"}
874897
self.db.test.insert(doc)

0 commit comments

Comments
 (0)