官方文档中,find 函数中的说明表明,在 find 中传参应该和 cursor 后面调用 sort 函数一样
升序:pymongo.ASCENDING ( 1 )
降序:pymongo.DESCENDING ( -1 )
find(filter=None, projection=None, skip=0, limit=0, no_cursor_timeout=False, cursor_type=CursorType.NON_TAILABLE, sort=None, allow_partial_results=False, oplog_replay=False, modifiers=None, manipulate=True)
sort 作为入参查询方式
collection = db_conn[collection_name]
result = collection.find( query_conditioin, projection={"_id":True, "dev_status":True, "created_at":True}, limit=3, sort=[("created_at",pymongo.ASCENDING)] )
sort 函数调用方式
collection = db_conn[collection_name]
result = collection.find( query_conditioin, projection={"_id":True, "dev_status":True, "created_at":True}).limit(3).sort([("created_at",pymongo.ASCENDING)])
官方源码参数详解:
filter
(optional): a SON object specifying elements which
must be present for a document to be included in the
result setprojection
(optional): a list of field names that should be
returned in the result set or a dict specifying the fields
to include or exclude. Ifprojection
is a list “_id” will
always be returned. Use a dict to exclude fields from
the result (e.g. projection={‘_id’: False}).skip
(optional): the number of documents to omit (from
the start of the result set) when returning the resultslimit
(optional): the maximum number of results to
returnno_cursor_timeout
(optional): if False (the default), any
returned cursor is closed by the server after 10 minutes of
inactivity. If set to True, the returned cursor will never
time out on the server. Care should be taken to ensure that
cursors with no_cursor_timeout turned on are properly closed.cursor_type
(optional): the type of cursor to return. The valid
options are defined by :class:~pymongo.cursor.CursorType
:- :attr:
~pymongo.cursor.CursorType.NON_TAILABLE
- the result of
this find call will return a standard cursor over the result set. - :attr:
~pymongo.cursor.CursorType.TAILABLE
- the result of this
find call will be a tailable cursor - tailable cursors are only
for use with capped collections. They are not closed when the
last data is retrieved but are kept open and the cursor location
marks the final document position. If more data is received
iteration of the cursor will continue from the last document
received. For details, see thetailable cursor documentation
_.
<http://www.mongodb.org/display/DOCS/Tailable+Cursors> - :attr:
~pymongo.cursor.CursorType.TAILABLE_AWAIT
- the result
of this find call will be a tailable cursor with the await flag
set. The server will wait for a few seconds after returning the
full result set so that it can capture and return additional data
added during the query. - :attr:
~pymongo.cursor.CursorType.EXHAUST
- the result of this
find call will be an exhaust cursor. MongoDB will stream batched
results to the client without waiting for the client to request
each batch, reducing latency. See notes on compatibility below.
- :attr:
sort
(optional): a list of (key, direction) pairs
specifying the sort order for this query. See
:meth:~pymongo.cursor.Cursor.sort
for details.allow_partial_results
(optional): if True, mongos will return
partial results if some shards are down instead of returning an
error.oplog_replay
(optional): If True, set the oplogReplay query
flag.batch_size
(optional): Limits the number of documents returned in
a single batch.manipulate
(optional): DEPRECATED - If True (the default),
apply any outgoing SON manipulators before returning.collation
(optional): An instance of
:class:~pymongo.collation.Collation
. This option is only supported
on MongoDB 3.4 and above.return_key
(optional): If True, return only the index keys in
each document.show_record_id
(optional): If True, adds a field$recordId
in
each document with the storage engine’s internal record identifier.snapshot
(optional): If True, prevents the cursor from returning
a document more than once because of an intervening write
operation.hint
(optional): An index, in the same format as passed to
:meth:~pymongo.collection.Collection.create_index
(e.g.
[('field', ASCENDING)]
). Pass this as an alternative to calling
:meth:~pymongo.cursor.Cursor.hint
on the cursor to tell Mongo the
proper index to use for the query.max_time_ms
(optional): Specifies a time limit for a query
operation. If the specified time is exceeded, the operation will be
aborted and :exc:~pymongo.errors.ExecutionTimeout
is raised. Pass
this as an alternative to calling
:meth:~pymongo.cursor.Cursor.max_time_ms
on the cursor.max_scan
(optional): The maximum number of documents to scan.
Pass this as an alternative to calling
:meth:~pymongo.cursor.Cursor.max_scan
on the cursor.min
(optional): A list of field, limit pairs specifying the
inclusive lower bound for all keys of a specific index in order.
Pass this as an alternative to calling
:meth:~pymongo.cursor.Cursor.min
on the cursor.max
(optional): A list of field, limit pairs specifying the
exclusive upper bound for all keys of a specific index in order.
Pass this as an alternative to calling
:meth:~pymongo.cursor.Cursor.max
on the cursor.comment
(optional): A string or document. Pass this as an
alternative to calling :meth:~pymongo.cursor.Cursor.comment
on the
cursor.modifiers
(optional): DEPRECATED - A dict specifying
additional MongoDB query modifiers. Use the keyword arguments listed
above instead.