当我需要更多/全部(从2.6.5迁移到3.1.2)时,Mongoose将查询限制为1000个结果

时间:2012-09-28 15:45:31

标签: node.js mongodb mongoose

我正在将我的应用程序从Mongoose 2.6.5迁移到3.1.2,并且我遇到了一些意想不到的行为。也就是说,我注意到查询结果自动限制为1000条记录,而其他所有内容的工作方式都相同。在我的代码(下面)中,我设置了一个值maxIvDataPoints,它限制了返回的数据点的数量(并最终发送到客户端浏览器),并且该值在别处设置为1500.我使用计数查询来确定潜在结果的总数,然后是后续的mod,使用count和maxIvDataPoints的值来限制实际查询结果,以确定mod的值。我正在运行节点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()

2 个答案:

答案 0 :(得分:9)

你被一对相关因素绊倒了:

  1. Mongoose的默认查询batchSize changed to 1000 in 3.1.2
  2. MongoDB有一个known issue,其中需要内存排序的查询会将查询批量大小的硬限制放在返回的文档数上。
  3. 因此,您可以选择在TestDataPoint上放置一个组合索引,允许mongo在此类查询中使用它来dataPoint进行排序,或者将batch size增加到至少您期望的文件总数。

答案 1 :(得分:4)

哇,太可怕了。我将很快发布修复mongoose删除batchSize默认值(在流式传输大型结果集时很有帮助)。谢谢你的指针。

更新:3.2.1和2.9.1已随修复程序一起发布(默认情况下已删除batchSize)。