MongoDB聚合 - 从给定数组创建新数组

时间:2018-05-09 09:14:28

标签: mongodb aggregate aggregation

我希望根据我的聚合结果从给定数组创建新数组,以下文档中的所需结果是将数组拆分为新数组,其中拆分点是操作&#34; start&#34; < / p>

{
    "_id" : "5f851b06b08ab4b1f916c14841d4bbba",
    "actions" : [
        {
            "action" : "start",
            "datetime" : 1525692527345.0
        },
        {
            "action" : "scrolled",
            "datetime" : 1525692545966.0
        },
        {
            "action" : "scrolled",
            "datetime" : 1525692545983.0
        },
        {
            "action" : "click",
            "datetime" : 1525692545999.0
        },
        {
            "action" : "start",
            "datetime" : 1525693343877.0
        },
        {
            "action" : "mousemove",
            "datetime" : 1525693351075.0
        },
        {
            "action" : "mousemove",
            "datetime" : 1525693351117.0
        },
        {
            "action" : "scrolled",
            "datetime" : 1525693351212.0
        },
        {
            "action":   "scrolled",
            "datetime": 1525693354026.0
        },
        {
            "action":   "scrolled",
            "datetime": 1525693354126.0
        }
    ]
}

所以接下来我应该有一个看起来像这样的文件:

{
    "_id":      "5f851b06b08ab4b1f916c14841d4bbba",
    "session1": [
        {
            "action":   "start",
            "datetime": 1525692527345.0
        },
        {
            "action":   "scrolled",
            "datetime": 1525692545966.0
        },
        {
            "action":   "scrolled",
            "datetime": 1525692545983.0
        },
        {
            "action":   "click",
            "datetime": 1525692545999.0
        }
    ],
    "session2": [
        {
            "action":   "start",
            "datetime": 1525693343877.0
        },
        {
            "action":   "mousemove",
            "datetime": 1525693351075.0
        },
        {
            "action":   "mousemove",
            "datetime": 1525693351118.0
        },
        {
            "action":   "mousemove",
            "datetime": 1525693351119.0
        },
        {
            "action":   "mousemove",
            "datetime": 1525693351121.0
        },
        {
            "action":   "scrolled",
            "datetime": 1525693351212.0
        },
        {
            "action":   "scrolled",
            "datetime": 1525693354026.0
        },
        {
            "action":   "scrolled",
            "datetime": 1525693354126.0
        }
    ]
}

session1session2字段名称可以是任何它只是为了显示所需的结果。

如何将此添加到我现有的聚合管道?

db.collection.aggregate(
    [
        { 
            "$match" : {
                "action" : {
                    "$exists" : true
                }, 
                "domain" : "domain.com"
            }
        }, 
        { 
            "$unwind" : "$action"
        }, 
        { 
            "$sort" : {
                "action.datetime" : 1.0
            }
        }, 
        { 
            "$group" : {
                "_id" : "$id", 
                "count" : {
                    "$sum" : 1.0
                }, 
                "actions" : {
                    "$addToSet" : {
                        "id" : "$id", 
                        "action" : "$action.action", 
                        "datetime" : "$action.datetime"
                    }
                }
            }
        }, 
        { 
            "$unwind" : "$actions"
        }, 
        { 
            "$sort" : {
                "actions.datetime" : 1.0
            }
        }, 
        { 
            "$group" : {
                "_id" : "$actions.id", 
                "count" : {
                    "$sum" : 1.0
                }, 
                "actions" : {
                    "$push" : {
                        "action" : "$actions.action", 
                        "datetime" : "$actions.datetime"
                    }
                }
            }
        }, 
        { 
            "$match" : {
                "count" : {
                    "$gt" : 1.0
                }
            }
        }
    ], 
    { 
        "allowDiskUse" : true
    }
);

感谢您的期待!

0 个答案:

没有答案