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_Mongodb Query_Aggregation Framework - Fatal编程技术网

Mongodb 数组中的多个嵌套组

Mongodb 数组中的多个嵌套组,mongodb,mongodb-query,aggregation-framework,Mongodb,Mongodb Query,Aggregation Framework,我在MongoDB中有一组元素,如下所示: /* 1 */ { "_id" : ObjectId("58736c7f7d43c305461cdb9b"), "Name" : "Kevin", "pb_event" : [ { "event_type" : "Birthday", "event_date" : "2014-08-31" }, { "ev

我在MongoDB中有一组元素,如下所示:

/* 1 */
{
    "_id" : ObjectId("58736c7f7d43c305461cdb9b"),
    "Name" : "Kevin",
    "pb_event" : [ 
        {
            "event_type" : "Birthday",
            "event_date" : "2014-08-31"
        }, 
        {
            "event_type" : "Anniversary",
            "event_date" : "2014-08-31"
        }
    ]
}

/* 2 */
{
    "_id" : ObjectId("58736cfc7d43c305461cdba8"),
    "Name" : "Peter",
    "pb_event" : [ 
        {
            "event_type" : "Birthday",
            "event_date" : "2014-08-31"
        }, 
        {
            "event_type" : "Anniversary",
            "event_date" : "2015-03-24"
        }
    ]
}

/* 3 */
{
    "_id" : ObjectId("58736cfc7d43c305461cdba9"),
    "Name" : "Pole",
    "pb_event" : [ 
        {
            "event_type" : "Birthday",
            "event_date" : "2015-03-24"
        }, 
        {
            "event_type" : "Work Anniversary",
            "event_date" : "2015-03-24"
        }
    ]
}
现在我想要的结果是,在
event\u date
上分组,然后在
event\u type
上分组<代码>事件类型包含相关用户的所有名称,然后计算相应数组中的记录数

预期产出

/* 1 */
{    
    "event_date" : "2014-08-31",
    "data" : [ 
        {
            "event_type" : "Birthday",
            "details" : [ 
                {
                    "_id" : ObjectId("58736c7f7d43c305461cdb9b"),
                    "name" : "Kevin"
                }, 
                {
                    "_id" : ObjectId("58736cfc7d43c305461cdba8"),
                    "name" : "Peter"
                }
            ],
            "count" : 2
        }, 
        {
            "event_type" : "Anniversary",
            "details" : [ 
                {
                    "_id" : ObjectId("58736c7f7d43c305461cdb9b"),
                    "name" : "Kevin"
                }
            ],
            "count" : 1
        }
    ]
}

/* 2 */
{
    "event_date" : "2015-03-24",
    "data" : [ 
        {
            "event_type" : "Anniversary",
            "details" : [ 
                {
                    "_id" : ObjectId("58736cfc7d43c305461cdba8"),
                    "name" : "Peter"
                }
            ],
            "count" : 1
        }, 
        {
            "event_type" : "Birthday",
            "details" : [ 
                {
                    "_id" : ObjectId("58736cfc7d43c305461cdba9"),
                    "name" : "Pole"
                }
            ],
            "count" : 1
        }, 
        {
            "event_type" : "Work Anniversary",
            "details" : [ 
                {
                    "_id" : ObjectId("58736cfc7d43c305461cdba9"),
                    "name" : "Pole"
                }
            ],
            "count" : 1
        }
    ]
}

使用聚合框架,您需要运行具有以下阶段的管道,以便获得所需的结果:

db.collection.aggregate([
    { "$unwind": "$pb_event" },
    {
        "$group": {
            "_id": {
                "event_date": "$pb_event.event_date",
                "event_type": "$pb_event.event_type" 
            },            
            "details": {
                "$push": {
                    "_id": "$_id",
                    "name": "$Name"
                }
            },
            "count": { "$sum": 1 }            
        }
    },    
    {
        "$group": {
            "_id": "$_id.event_date",            
            "data": {
                "$push": {
                    "event_type": "$_id.event_type",
                    "details": "$details",
                    "count": "$count"
                }
            }           
        }
    },
    {
        "$project": {
            "_id": 0,
            "event_date": "$_id",
            "data": 1
        }
    }
])

在上述管道中,第一步是操作符

{ "$unwind": "$pb_event" }
当数据存储为数组时,这非常方便。当对列表数据字段应用“展开”操作符时,它将为应用“展开”的列表数据字段的每个元素生成新记录。它基本上使数据平坦化

这是下一个管道阶段的必要操作,即步骤,在该步骤中,您可以通过解构的
pb\u事件
数组字段
event\u date
event\u type
对展开的文档进行分组:

{
    "$group": {
        "_id": {
            "event_date": "$pb_event.event_date",
            "event_type": "$pb_event.event_type" 
        },            
        "details": {
            "$push": {
                "_id": "$_id",
                "name": "$Name"
            }
        },
        "count": { "$sum": 1 }            
    }
},
管道操作符类似于SQL的
GROUPBY
子句。在SQL中,除非使用任何聚合函数,否则不能使用
groupby
。同样,您还必须在MongoDB中使用聚合函数(称为累加器操作符)。您可以阅读有关聚合函数的更多信息

在该操作中,使用累加器运算符计算计数合计的逻辑,即组中的文档总数。在同一管道中,您可以使用运算符聚合
名称
\u id
子文档的列表,该运算符返回每个组的表达式值数组

前面的管道

{
    "$group": {
        "_id": "$_id.event_date",            
        "data": {
            "$push": {
                "event_type": "$_id.event_type",
                "details": "$details",
                "count": "$count"
            }
        }           
    }
}
将通过在
事件_日期
上分组,进一步聚合上一个管道的结果,通过使用创建新的数据列表,然后使用最终的管道阶段,形成所需输出的基础

{
    "$project": {
        "_id": 0,
        "event_date": "$_id",
        "data": 1
    }
}

通过将
\u id
字段重命名为
event\u date
并保留另一个字段来重塑文档字段的形状。

@charidam..这可以在上述查询中按月日期分组吗?