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