我正在将我的应用程序从Mongoose 2.6.5迁移到3.1.2,并且遇到了一些意外行为。也就是说,我注意到查询结果自动被限制为1000条记录,而几乎所有其他功能都一样。在我的代码中(下面),我设置了一个maxIvDataPoints值,该值限制返回的数据点数(最终发送到客户端浏览器),并且该值在其他地方设置为1500。我使用一个计数查询来确定潜在的总数结果,然后是一个后续mod,它使用maxIvDataPoints的计数和值来限制实际查询结果,以确定mod的值。我正在运行 Node 0.8.4和mongo 2.0.4,并在coffeescript中编写服务器端代码。

在安装mongoose 3.1.x之前,该代码可以按照我的要求工作,每次返回的数据点都少于1500。在安装3.1.2之后,我每次都会准确返回1000个数据点(假设在指定范围内有1000个以上的数据点)。结果将被截断,因此数据点1001至〜1500是不再返回的数据点。

似乎在某些地方可以控制这种行为,但是我在文档中,此处或Google群组中找不到任何内容。我还是n00b的亲戚,所以我可能错过了一些明显的事情。

DataManager::ivDataQueryStream = (testId, minTime, maxTime, callback) ->

    # If minTime and maxTime have been provided, set a flag to limit time extents of query
    unless isNaN(minTime)
    timeLimits = true

    # Load the max number of IV data points to be displayed from CONFIG
    maxIvDataPoints = CONFIG.maxIvDataPoints

    # Construct a count query to determine the number if IV data points in range
    ivCountQuery = TestDataPoint.count({})
    ivCountQuery.where "testId", testId

    if timeLimits
        ivCountQuery.gt "testTime", minTime
        ivCountQuery.lt "testTime", maxTime

    ivCountQuery.exec (err, count) ->

        ivDisplayQuery = TestDataPoint.find({})
        ivDisplayQuery.where "testId", testId

        if timeLimits
            ivDisplayQuery.gt "testTime", minTime
            ivDisplayQuery.lt "testTime", maxTime

        # If the data set is too large, use modulo to sample, keeping the total data series
        # for display below maxIvDataPoints
        if count > maxIvDataPoints
            dataMod = Math.ceil count/maxIvDataPoints

            ivDisplayQuery.mod "dataPoint", dataMod, 1

        ivDisplayQuery.sort "dataPoint" #, 1 <-- new sort syntax for Mongoose 3.x
        callback ivDisplayQuery.stream()

最佳答案

您会被以下两个相关因素绊倒:

  • Mongoose 的默认查询batchSize changed to 1000 in 3.1.2
  • MongoDB有一个known issue,其中要求进行内存排序的查询对返回的文档数量施加了查询批处理大小的硬限制。

  • 因此,您的选择是在TestDataPoint上放置一个组合索引,以使mongo可以在这种类型的查询中使用它来按dataPoint进行排序,或者将batch size增加到至少期望的文档总数。

    关于node.js - 当我想要更多/全部时, Mongoose 将查询限制为1000个结果(从2.6.5迁移到3.1.2),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12643195/

    10-09 01:27
    查看更多