Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MongoDB,用另一个键值对内部哈希键值进行分组_Mongodb_Aggregation Framework - Fatal编程技术网

MongoDB,用另一个键值对内部哈希键值进行分组

MongoDB,用另一个键值对内部哈希键值进行分组,mongodb,aggregation-framework,Mongodb,Aggregation Framework,我的收藏中有以下4个元素: /* 1 */ { "demographics": [ { "key": "country", "value": "ES" }, { "key": "city", "value": "Sevilla" }, { "key": "region", "value": "Andalucía" } ] } /* 2 */ { "demograp

我的收藏中有以下4个元素:

/* 1 */
{
  "demographics": [
    {
      "key": "country",
      "value": "ES"
    },
    {
      "key": "city",
      "value": "Sevilla"
    },
    {
      "key": "region",
      "value": "Andalucía"
    }
  ]
}

/* 2 */
{
  "demographics": [
    {
      "key": "city",
      "value": "Cádiz"
    },
    {
      "key": "country",
      "value": "ES"
    },
    {
      "key": "region",
      "value": "Andalucía"
    }
  ]
}

/* 3 */
{
  "demographics": [
    {
      "key": "country",
      "value": "GB"
    },
    {
      "key": "region",
      "value": "Greater London"
    },
    {
      "key": "city",
      "value": "London"
    }
  ]
}

/* 4 */
{
  "demographics": [
    {
      "key": "country",
      "value": "ES"
    },
    {
      "key": "region",
      "value": "Andalucía"
    },
    {
      "key": "city",
      "value": "Sevilla"
    }
  ]
}
我想按以下方式将其分组:

  • demographic.value
    demographic.key=“country”
  • demographic.value
    demographic.key=“region”
  • demographic.value
    demographic.key=“city”
有这样的结果:

{ "values": ["ES", "Andalucía", "Sevilla"], "count": 2 }
{ "values": ["ES", "Andalucía", "Cádiz"], "count": 1 }
{ "values": ["GB", "Greater London", "London"], "count": 1 }
注意:注意
人口统计
数组元素的顺序可能并不总是相同的

我试过了 这给了我一个结果:

/* 1 */
{
  "_id": "country",
  "values": [
    {
      "value": "GB",
      "count": 1.0
    },
    {
      "value": "ES",
      "count": 3.0
    }
  ]
}

/* 2 */
{
  "_id": "region",
  "values": [
    {
      "value": "Greater London",
      "count": 1.0
    },
    {
      "value": "Andalucía",
      "count": 3.0
    }
  ]
}

/* 3 */
{
  "_id": "city",
  "values": [
    {
      "value": "London",
      "count": 1.0
    },
    {
      "value": "Cádiz",
      "count": 1.0
    },
    {
      "value": "Sevilla",
      "count": 2.0
    }
  ]
}

但这不是我要找的组

您可以尝试运行以下管道:

db.test.aggregate([
    { "$unwind": "$demographics" },
    { "$sort": { "demographics.key": 1, "demographics.value": 1 } },
    {
        "$group": {
            "_id": "$_id",
            "values": { "$push": "$demographics.value" }
        }
    },
    {  
        "$group": {
            "_id": "$values",
            "count": { "$sum": 1 }
        }
    },
    {
        "$project": {
            "_id": 0, "values": "$_id", "count": 1
        }
    }
])
样本输出

/* 1 */
{
    "count" : 2,
    "values" : [ 
        "Sevilla", 
        "ES", 
        "Andalucía"
    ]
}

/* 2 */
{
    "count" : 1,
    "values" : [ 
        "London", 
        "GB", 
        "Greater London"
    ]
}

/* 3 */
{
    "count" : 1,
    "values" : [ 
        "Cádiz", 
        "ES", 
        "Andalucía"
    ]
}

这是假设
人口统计
数组元素的顺序总是相同的,不幸的是,在实际环境中可能不是这样。我已经更新了这个问题,以考虑到这个角落的情况。@fguillen你是对的。我已经在
$group
阶段之前添加了一个排序管道来处理这个角落案例。很漂亮,
sort
命令实现了一个我不希望有用的技巧。我现在尝试将输出转换为自然顺序
国家/地区/城市
,但这没关系,我可以在Mongo中完成。
/* 1 */
{
    "count" : 2,
    "values" : [ 
        "Sevilla", 
        "ES", 
        "Andalucía"
    ]
}

/* 2 */
{
    "count" : 1,
    "values" : [ 
        "London", 
        "GB", 
        "Greater London"
    ]
}

/* 3 */
{
    "count" : 1,
    "values" : [ 
        "Cádiz", 
        "ES", 
        "Andalucía"
    ]
}