我的收藏集中有约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个不同的分组计数器:
demographics.key
db.panelUsers.aggregate(
[
{
$unwind: {
path: "$demographics"
}
},
{
$group: {
_id: {
demographic_key: "$demographics.key"
},
"count": {
$sum: 1
}
}
}
]
)
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()
时,我看不到要使用的索引:
{
"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
}
{
"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
}
如何为这些查询建立索引,或者我必须在查询中进行哪些更改才能使用索引?