sort參數與sort函數
官方文檔中,find函數中的說明表明,在find中傳參應該和cursor后面調用sort函數一樣
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 (optional): a list of (key, direction) pairs specifying the sort order for this query. See
sort()for details.
查詢條件和排序條件如下
query = {
# 'start': {'$lte': int_ip},
'end': {'$gte': int_ip}
}
sort = [('end', 1)]
sort作為入參查詢方式
cursor = db.aiwen.find(query, sort=sort, limit=1)
sort函數調用方式
db.aiwen.find(query).sort([('end', 1)]).limit(1)
為了保證格式統一,將find_one替換成find函數
sort參數方式調用 耗時 cost:1.97000002861
sort函數方式調用 耗時 cost:1.97200012207
但用Robomongo工具查詢,發現時延很短
find_one函數
cursor = db.aiwen.find_one(query, sort=sort)
發現查詢結果一下得到提升,與robomongo相當
cost:0.00399994850159

