MongoDB Query部分忽略了限制

时间:2015-04-13 20:52:17

标签: node.js mongodb limit mongodb-query

我正在运行一个查询来尝试从一个集合中获取25条记录,这些记录包含大约3-4k条存储记录,每条记录都相当大。我需要这些按特定的Id,升序排序。所以我创建了这个查询:

var collection = mongoDb.collection('matches');

collection
    .find({
        matchId: {$exists:true}
    })
    .sort({matchId: 1})
    .limit(25)
    .toArray(function(err, docs) {

        /* error management */

        doIf(isVerbose, logger.info, 'Found {magenta:%s} records.', docs.length);

    });

我遇到的奇怪问题是它返回 22 记录。当我交换方向(asc到desc)时,它返回了完整的25。

我查了一下,看到如果我为升序搜索添加索引可能会更好,所以我将其添加到mongo CLI中:

db.matches.createIndex({matchId:1});

在我的节点应用中再次测试它。这一次(以及之后的所有尝试),我的查询返回 45 记录。

如果我将限制更改为20,21或22,它们都会很好地返回 - 23以后似乎返回的数量大于限制。

奇怪的是,我可以在mongo CLI中运行此查询,它将按预期返回25条记录。

我想知道我在这里遗失了什么?

如果它有价值,这是我的收藏品的统计数据:

> db.matches.stats();
{
    "ns" : "[database].matches",
    "count" : 3678,
    "size" : 910236192,
    "avgObjSize" : 247481,
    "numExtents" : 16,
    "storageSize" : 1069953024,
    "lastExtentSize" : 280522752,
    "paddingFactor" : 1,
    "paddingFactorNote" : "paddingFactor is unused and unmaintained in 3.0. It remains hard coded to 1.0 for compatibility only.",
    "userFlags" : 1,
    "capped" : false,
    "nindexes" : 2,
    "totalIndexSize" : 245280,
    "indexSizes" : {
            "_id_" : 130816,
            "matchId_1" : 114464
    },
    "ok" : 1
}

编辑:请求评论。

以下是节点应用程序的解释(返回45个结果):

{ queryPlanner:
   { plannerVersion: 1,
     namespace: '[db].matches',
     indexFilterSet: false,
     parsedQuery: { matchId: [Object] },
     winningPlan: { stage: 'LIMIT',
         limitAmount: 0,
         inputStage:
           { stage: 'FETCH',
             filter: { matchId: [Object] },
             inputStage:
              { stage: 'IXSCAN',
                keyPattern: [Object],
                indexName: 'matchId_1',
                isMultiKey: false,
                direction: 'forward',
                indexBounds: [Object] } } }
     rejectedPlans: [] },
  executionStats:
   { executionSuccess: true,
     nReturned: 1,
     executionTimeMillis: 0,
     totalKeysExamined: 1,
     totalDocsExamined: 1,
     executionStages:
      { stage: 'LIMIT',
        nReturned: 1,
        executionTimeMillisEstimate: 0,
        works: 2,
        advanced: 1,
        needTime: 0,
        needFetch: 0,
        saveState: 0,
        restoreState: 0,
        isEOF: 1,
        invalidates: 0,
        limitAmount: 0,
        inputStage: [Object] },
     allPlansExecution: [] },
  serverInfo:
   { host: '[Host Name]',
     port: 27017,
     version: '3.0.2',
     gitVersion: '6201872043ecbbc0a4cc169b5482dcf385fc464f' } }

这是来自终端的那个(返回25)

    "queryPlanner" : {
            "plannerVersion" : 1,
            "namespace" : "[db].matches",
            "indexFilterSet" : false,
            "parsedQuery" : {
                    "$and" : [ ]
            },
            "winningPlan" : {
                    "stage" : "LIMIT",
                    "limitAmount" : 25,
                    "inputStage" : {
                            "stage" : "PROJECTION",
                            "transformBy" : {
                                    "$limit" : 25
                            },
                            "inputStage" : {
                                    "stage" : "FETCH",
                                    "inputStage" : {
                                            "stage" : "IXSCAN",
                                            "keyPattern" : {
                                                    "matchId" : 1
                                            },
                                            "indexName" : "matchId_1",
                                            "isMultiKey" : false,
                                            "direction" : "forward",
                                            "indexBounds" : {
                                                    "matchId" : [
                                                            "[MinKey, MaxKey]"
                                                    ]
                                            }
                                    }
                            }
                    }
            },
            "rejectedPlans" : [ ]
    },
    "serverInfo" : {
            "host" : "[hostName]",
            "port" : 27017,
            "version" : "3.0.2",
            "gitVersion" : "6201872043ecbbc0a4cc169b5482dcf385fc464f"
    },
    "ok" : 1

}

0 个答案:

没有答案