如果从索引中检索数据,MongoDB查询是否需要显式sort()调用?

时间:2018-06-26 19:06:22

标签: python mongodb pymongo

我正在构建一个mongo数据库,用于存储带有时间戳的数据。我数据库中的每个文档都有一个时间字段:

{"time":<datetime-object>}

我已经为time字段创建了索引:

self.db.test.create_index([("time", pymongo.ASCENDING)])

并执行一个查询,该查询仅从数据库请求时间戳信息:

self.db.test.find({'time':{'$gte':start, '$lte':end}}, {"time":1, "_id":0}).sort([("time", 1)])

我还阅读了其他问题/文档,说使用索引来获取文档应按排序顺序返回文档,因为索引本身已被排序,但是我看到的所有示例仍然直接调用{{1} }作为查询的一部分。我的问题是,如果我仅从数据库中仅请求一个我有索引的字段,我是否需要在查询中包括sort()方法,还是将文件按排序顺序返回?

1 个答案:

答案 0 :(得分:1)

  

如果我仅从数据库中仅请求一个具有索引的字段,我是否需要在我的查询中包括sort()方法,还是将文档按排序顺序返回?

在示例single field中,其中索引为covered query的情况下,返回的顺序就是索引本身的顺序。

但是,对于带有multikey index的多键字段,情况并非如此。这是因为多键索引无法覆盖对数组字段的查询。

由于以下原因,建议指定sort()

  • query planner如果能够使用索引,则会自动放弃排序阶段。有关更多信息,另请参见Query OptimisationExplain Results

  • 明确指定Sub FindFormatting() Dim Found As Range Application.FindFormat.Clear Application.FindFormat.Interior.Color = RGB(0, 106, 130) Set Found = FindAll(What:="", SearchWhat:=ActiveSheet, LookIn:=xlFormulas, LookAt:=xlPart, SearchFormat:=True) If Not Found Is Nothing Then Found.Font.Color = RGB(255, 255, 255) Application.FindFormat.Interior.Color = RGB(0, 138, 170) Set Found = FindAll(What:="", SearchWhat:=ActiveSheet, LookIn:=xlFormulas, LookAt:=xlPart, SearchFormat:=True) If Not Found Is Nothing Then Found.Font.Color = RGB(255, 255, 255) Application.FindFormat.Interior.Color = RGB(177, 209, 217) Set Found = FindAll(What:="", SearchWhat:=ActiveSheet, LookIn:=xlFormulas, LookAt:=xlPart, SearchFormat:=True) If Not Found Is Nothing Then Found.Font.Color = RGB(0, 0, 0) Application.FindFormat.Interior.Color = RGB(204, 225, 230) Set Found = FindAll(What:="", SearchWhat:=ActiveSheet, LookIn:=xlFormulas, LookAt:=xlPart, SearchFormat:=True) If Not Found Is Nothing Then Found.Font.Color = RGB(0, 0, 0) End Sub Function FindAll(What, _ Optional SearchWhat As Variant, _ Optional LookIn, _ Optional LookAt, _ Optional SearchOrder, _ Optional SearchDirection As XlSearchDirection = xlNext, _ Optional MatchCase As Boolean = False, _ Optional MatchByte, _ Optional SearchFormat) As Range 'LookIn can be xlValues or xlFormulas, _ LookAt can be xlWhole or xlPart, _ SearchOrder can be xlByRows or xlByColumns, _ SearchDirection can be xlNext, xlPrevious, _ MatchCase, MatchByte, and SearchFormat can be True or False. _ Before using SearchFormat = True, specify the appropriate settings for the Application.FindFormat _ object; e.g. Application.FindFormat.NumberFormat = "General;-General;""-""" Dim SrcRange As Range If IsMissing(SearchWhat) Then Set SrcRange = ActiveSheet.UsedRange ElseIf TypeOf SearchWhat Is Range Then Set SrcRange = IIf(SearchWhat.Cells.Count = 1, SearchWhat.Parent.UsedRange, SearchWhat) ElseIf TypeOf SearchWhat Is Worksheet Then Set SrcRange = SearchWhat.UsedRange Else: SrcRange = ActiveSheet.UsedRange End If If SrcRange Is Nothing Then Exit Function 'get the first matching cell in the range first With SrcRange.Areas(SrcRange.Areas.Count) Dim FirstCell As Range: Set FirstCell = .Cells(.Cells.Count) End With Dim CurrRange As Range: Set CurrRange = SrcRange.Find(What:=What, After:=FirstCell, LookIn:=LookIn, LookAt:=LookAt, _ SearchDirection:=SearchDirection, MatchCase:=MatchCase, MatchByte:=MatchByte, SearchFormat:=SearchFormat) If Not CurrRange Is Nothing Then Set FindAll = CurrRange Do Set CurrRange = SrcRange.Find(What:=What, After:=CurrRange, LookIn:=LookIn, LookAt:=LookAt, _ SearchDirection:=SearchDirection, MatchCase:=MatchCase, MatchByte:=MatchByte, SearchFormat:=SearchFormat) If CurrRange Is Nothing Then Exit Do If Application.Intersect(FindAll, CurrRange) Is Nothing Then Set FindAll = Application.Union(FindAll, CurrRange) Else: Exit Do End If Loop End If End Function 不仅可以保护您的代码以防意外(例如,不一致的值等),还可以使代码可读。

您可能还对Use Indexes to Sort Query Results

感兴趣