使用mongoose的多个组和项目

时间:2017-12-20 11:34:17

标签: node.js mongodb mongoose

我正在尝试在Mongoose中按时间范围显示聚合数据。我的数据就像:

{company: "1" createdTime:"2017-01-01"}
{company: "1" createdTime:"2017-01-01"}

{company: "1" createdTime:"2017-01-02"}
{company: "2" createdTime:"2017-01-02"}

{company: "2" createdTime:"2017-01-03"}
{company: "3" createdTime:"2017-01-03"}
{company: "3" createdTime:"2017-01-03"}

我创建了一个类似的查询:

{
    "aggregate": [{
            "$project": {
                "frequency": {
                    "$dateToString": {
                        "format": "%Y-%m-%d",
                        "date": "$createdTime"
                    }
                }
            }
        }, {
            "$group": {
                "_id": {
                    "date": "$frequency"
                },
                "count": {
                    "$sum": 1
                }
            }
        }
    ]
}

并返回:

[
    {
        "_id": {
            "date": "2017-01-01"
        },
        "count": 2
    },
    {
        "_id": {
            "date": "2017-01-02"
        },
        "count": 2
    },
    {
        "_id": {
            "date": "2017-01-03"
        },
        "count": 3
    }
]

这给了我对该特定日期总体事件的计数,这很好。

我想进一步打破这一点,以便给出每个公司在该日期出现的总和,它应该是这样的:

[
    {
        "_id": {
            "date": "2017-01-01",
            "companies": [{
                "company": 1,
                "count": 2
            }]
        },
        "count": 2
    },
    {
        "_id": {
            "date": "2017-01-02",
            "companies": [{
                "company": 1,
                "count": 1
            },{
                "company": 2,
                "count": 1
            }]
        },
        "count": 2
    },
    {
        "_id": {
            "date": "2017-01-03",
            ,
            "companies": [{
                "company": 2,
                "count": 1
            },{
                "company": 3,
                "count": 2
            }]]
        },
        "count": 3
    }
]

我尝试过所有类型的组合,但是当我放入第二组时我总是会遇到错误,我无法知道如何做到这一点?

修改

我无法使用评论中的示例,或者它们不能用于我的用例,但是,结合这些信息,我提出了这个:

{
    "aggregate": [{
            "$group": {
                "_id": {
                    "company": "$company",
                    "date": {
                        "$dayOfMonth": "$createdTime"
                    },
                    "month": {
                        "$month": "$createdTime"
                    },
                    "year": {
                        "$year": "$createdTime"
                    }

                },
                "count": {
                    "$sum": 1
                }
            }
        }, {
            "$group": {
                "_id": "$_id.company",
                "events": {
                    "$push": {
                        "date": "$_id.date",
                        "month": "$_id.month",
                        "year": "$_id.year",
                        "count": "$count"
                    }
                },
                "totalEvents": {
                    "$sum": "$count"
                }
            }
        }
    ]
}

会产生一些结果,如:

[
    {
        "_id": "1",
        "dateCount": [
            {
                "date": 3,
                "month": 12,
                "year": 2017,
                "count": 40
            },
            {
                "date": 10,
                "month": 12,
                "year": 2017,
                "count": 8
            }
        ]
    }
]

理想情况下,日期将是yyyy-mm-dd,但在查询中使用完整日期将包括时间也会导致每次不是日期的条目。我想现在这样做,我可以轻松地创建一个日期,虽然它有点麻烦,但它会做到更好的事情。

编辑2

[{
        "_id": ObjectId("5a38a03b33ccd28fb87f6dc9"),
        "createdBy": ObjectId("5a2e203fa567425b2cb411fc"),
        "appId": ObjectId("5a2e203fa567425b2cb411fb"),
        "eventName": "event",
        "userName": "person",
        "userEmail": "company__29",
        "organisation": "company",
        "module": "module",
        "createdTime": ISODate("2017-12-02T17:07:25.000Z")
        "__v": 0
    }
]

查询:

{
    "aggregate": [{
            "$project": {
                "frequency": {
                    "$dateToString": {
                        "format": "%U",
                        "date": "$createdTime"
                    }
                }
            }
        }, {
            "$group": {
                "_id": {
                    "frequency": "$frequency",
                    "organisation": "$organisation"
                },
                "count": {
                    "$sum": 1
                }
            }
        }, {
            "$group": {
                "_id": {
                    "date": "$_id.frequency"
                },
                "count": {
                    "$sum": 1
                },
                "companies": {
                    "$push": {
                        "organisation": "$organisation",
                        "count": "$count"
                    }
                }
            }
        }

    ]
}

0 个答案:

没有答案