MongoDB,内部对象和分组中的索引查询?

时间:2019-04-20 09:16:49

标签: mongodb indexing grouping aggregation

我的收藏集中有约700万个文档,我需要对其进行分组查询。

我有这样的文件

{
  "_id" : ObjectId("5cba0179577fa92b9b6b36e8"),
  "demographics" : [
    {
      "key" : "country_code",
      "value" : "DE"
    },
    {
      "key" : "gender",
      "value" : "male"
    }
  ]
},
{
  "_id" : ObjectId("5cba0179577fa92b9b6b36e9"),
  "demographics" : [
    {
      "key" : "country_code",
      "value" : "US"
    },
    {
      "key" : "gender",
      "value" : "female"
    },
    {
      "key" : "sport",
      "value" : "yes"
    }
  ]
}

我想做2个不同的分组计数器:

1。按demographics.key

计算文档数
db.panelUsers.aggregate(
  [
    {
      $unwind: {
        path: "$demographics"
      }
    },
    {
      $group: {
        _id: {
          demographic_key: "$demographics.key"
        },
        "count": {
          $sum: 1
        }
      }
    }
  ]
)

2。按组合demographics.key / demographics.value

计算文档数
db.panelUsers.explain().aggregate(
  [
    {
      $unwind: {
        path: "$demographics"
      }
    },
    {
      $group: {
        _id: {
          demographic_key: "$demographics.key",
          demographic_value: "$demopgraphics.value"
        },
        "count": {
          $sum: 1
        }
      }
    }
  ]
)

我创建了此索引以尝试使查询达到最佳状态:

{
  "v" : 2,
  "key" : {
    "demograhics.key" : 1.0,
    "demographics.value" : 1.0
  },
  "name" : "demograhics.key_1_demographics.value_1",
  "ns" : "panelhub.panelUsers"
}

但是当我查看两个查询的explain()时,我看不到要使用的索引:

1。

{
  "stages" : [
    {
      "$cursor" : {
        "query" : {},
        "fields" : {
          "demographics" : 1,
          "_id" : 0
        },
        "queryPlanner" : {
          "plannerVersion" : 1,
          "namespace" : "panelhub.panelUsers",
          "indexFilterSet" : false,
          "parsedQuery" : {},
          "winningPlan" : {
            "stage" : "COLLSCAN",
            "direction" : "forward"
          },
          "rejectedPlans" : []
        }
      }
    },
    {
      "$unwind" : {
        "path" : "$demographics"
      }
    },
    {
      "$group" : {
        "_id" : {
          "demographic_key" : "$demographics.key"
        },
        "count" : {
          "$sum" : {
            "$const" : 1.0
          }
        }
      }
    }
  ],
  "ok" : 1.0
}

2。

{
  "stages" : [
    {
      "$cursor" : {
        "query" : {},
        "fields" : {
          "demographics" : 1,
          "demopgraphics.value" : 1,
          "_id" : 0
        },
        "queryPlanner" : {
          "plannerVersion" : 1,
          "namespace" : "panelhub.panelUsers",
          "indexFilterSet" : false,
          "parsedQuery" : {},
          "winningPlan" : {
            "stage" : "COLLSCAN",
            "direction" : "forward"
          },
          "rejectedPlans" : []
        }
      }
    },
    {
      "$unwind" : {
        "path" : "$demographics"
      }
    },
    {
      "$group" : {
        "_id" : {
          "demographic_key" : "$demographics.key",
          "demographic_value" : "$demopgraphics.value"
        },
        "count" : {
          "$sum" : {
            "$const" : 1.0
          }
        }
      }
    }
  ],
  "ok" : 1.0
}

如何为这些查询建立索引,或者我必须在查询中进行哪些更改才能使用索引?

0 个答案:

没有答案