MongoDB在巨大的数据集上返回空结果

时间:2015-03-02 14:13:39

标签: json mongodb indexing

我正在运行MongoDB 3.1服务器,其中包含约1.6亿个文档。文档如下所示:

{
  "_id": { "$oid" : "123456789" },
  "lastChange": "2015-02-09 13:22:27",
  "startDate": "2015-02-09",
  "receiptNumber" : 296,
  "itemReceiptPositions": [
    {
        "itemId": 900064,
        "listPrice": 8.99
    }
  ]
}

我在“itemReceiptPositions.itemId”和“receiptNumber”上创建了索引:

{
  "v" : 1,
  "key" : {
    "receiptNumber" : 1
  },
  "name" : "receiptNumber_1",
  "ns" : "someDatabase.someCollection"
},

{
  "v" : 1,
  "key" : {
    "itemReceiptPositions.itemId" : 1
  },
  "name" : "itemReceiptPositions.itemId_1",
  "ns" : "someDatabase.someCollection"
}

当我通过itemReceiptPositions.itemId键搜索此特定文档时,MongoDB找不到该文档:

> db.someCollection.find( { "itemReceiptPositions.itemId" : 900064 }).count()
0

但每当我搜索关键字“receiptNumber”时,都会找到该文件!甚至是查询

> db.someCollection.find( { "itemReceiptPositions.itemId" : { "$gt" : 0 }}).count()

返回零结果。怎么会这样?

以下是两个查询的.explain()输出:

> db.someCollection.find( { "itemReceiptPositions.itemId" : 900064 } ).explain()
{
  "queryPlanner" : {
    "plannerVersion" : 1,
    "namespace" : "someDatabase.someCollection",
    "indexFilterSet" : false,
    "parsedQuery" : {
      "itemReceiptPositions.itemId" : {
        "$eq" : 900064
      }
    },
    "winningPlan" : {
      "stage" : "FETCH",
      "inputStage" : {
        "stage" : "IXSCAN",
        "keyPattern" : {
          "itemReceiptPositions.itemId" : 1
        },
        "indexName" : "itemReceiptPositions.itemId_1",
        "isMultiKey" : true,
        "direction" : "forward",
        "indexBounds" : {
          "itemReceiptPositions.itemId" : [
            "[900064.0, 900064.0]"
          ]
        }
      }
    },
    "rejectedPlans" : [ ]
  },
  "serverInfo" : {
    "host" : "someHost",
    "port" : 1234,
    "version" : "3.1.0-pre-",
    "gitVersion" : "bbd95ca6a8b538b4cffece0b9d9c3ed811a455a7"
  },
  "ok" : 1
}

> db.someCollection.find( { "receiptNumber" : 296 }).explain()
{
  "queryPlanner" : {
    "plannerVersion" : 1,
    "namespace" : "someDatabase.someCollection",
    "indexFilterSet" : false,
    "parsedQuery" : {
      "receiptNumber" : {
        "$eq" : 296
      }
    },
    "winningPlan" : {
      "stage" : "FETCH",
      "inputStage" : {
        "stage" : "IXSCAN",
        "keyPattern" : {
          "receiptNumber" : 1
        },
        "indexName" : "receiptNumber_1",
        "isMultiKey" : false,
        "direction" : "forward",
        "indexBounds" : {
          "receiptNumber" : [
            "[296.0, 296.0]"
          ]
        }
      }
    },
    "rejectedPlans" : [ ]
  },
  "serverInfo" : {
    "host" : "someHost",
    "port" : 1234,
    "version" : "3.1.0-pre-",
    "gitVersion" : "bbd95ca6a8b538b4cffece0b9d9c3ed811a455a7"
  },
  "ok" : 1
}

2 个答案:

答案 0 :(得分:1)

您需要使用查询运算符$elemMatch

db.someCollection.find( { "itemReceiptPositions" : {$elemMatch: {"itemId" : 900064}}})

答案 1 :(得分:0)

AH!发现它!

您错误地将E大写并且还在其中添加了额外的p,请检查您的解释查询:

db.someCollection.find( { "itemREceiptPopsitions.itemId" : 900064 } ).explain()

itemREceiptPopsitions...
     ^       ^

您可能也在原始查询中犯了这个错误

作为旁注,您的查询工作正常,我在try.mongodb.org中查看,亲眼看看:

enter image description here