MongoDB Aggregate中的分组

时间:2017-06-19 10:04:47

标签: mongodb mongoose aggregation-framework

我从MongoDB开始,我发现聚合函数很难理解。

我已经阅读了很多主题并尝试了很多东西,但是我仍然没有得到我想要的结果。

我想将一些事情组合在一起,根据几个匹配参数来概述事物的数量。

我的数据库:

{
"_id" : ObjectId("5943a4f015813165b6d2b79a"),
"productType" : "mail",
"productAmount" : 1,
"orderid" : "1497607274",
"location" : "99",
"sublocation" : "02",
"date" : ISODate("2017-01-01T09:01:13.000Z"),
"__v" : 0
}

/* 2 */
{
    "_id" : ObjectId("5943a4f915813165b6d2b79c"),
    "productType" : "mail",
    "productAmount" : 1,
    "orderid" : "1497607274",
    "location" : "99",
    "sublocation" : "01",
    "date" : ISODate("2017-01-01T09:01:13.000Z"),
    "__v" : 0
}

/* 3 */
{
    "_id" : ObjectId("5943a4fe15813165b6d2b79e"),
    "productType" : "mail",
    "productAmount" : 1,
    "orderid" : "1497607274",
    "location" : "25",
    "sublocation" : "01",
    "date" : ISODate("2017-01-01T09:01:13.000Z"),
    "__v" : 0
}

/* 4 */
{
    "_id" : ObjectId("5943a56d15813165b6d2b7a0"),
    "productType" : "mail",
    "productAmount" : 1,
    "orderid" : "1497607274",
    "location" : "99",
    "sublocation" : "01",
    "date" : ISODate("2017-01-01T09:01:13.000Z"),
    "__v" : 0
}

/* 5 */
{
    "_id" : ObjectId("5943a971a367b069bb3e863c"),
    "productType" : "mail",
    "productAmount" : 1,
    "orderid" : "1497607274",
    "location" : "98",
    "sublocation" : "01",
    "date" : ISODate("2017-01-01T09:01:13.000Z"),
    "__v" : 0
}

/* 6 */
{
    "_id" : ObjectId("5943d824900fce1821c5845d"),
    "productType" : "keychain",
    "productAmount" : 1,
    "orderid" : "1497607251",
    "location" : "25",
    "sublocation" : "01",
    "date" : ISODate("2017-01-01T09:01:13.000Z")
}

/* 7 */
{
    "_id" : ObjectId("5943d9ea900fce1821c5845e"),
    "productType" : "mail",
    "productAmount" : 1,
    "orderid" : "1497607251",
    "location" : "25",
    "sublocation" : "02",
    "date" : ISODate("2017-01-01T09:01:13.000Z")
}

我当前的查询:

db.getCollection('products').aggregate([
{ $group: { 
    _id : {
        "orderid" : "$orderid",
        "location" : "$location",
        "product" : "$productType",
        "sublocation" : "$sublocation",
        "date" : "$date",
        "aant" : { "$sum" : "$productAmount"}
    },
    "aantal" : { "$sum" : 1},
    "aantal2" : { "$sum" : "$productAmount"}
   }
},
{
    $group : {
        _id : "$_id.product", 
        "productType" : {
            "$push" : {
                "loc" : "$_id.location",
                "prodtype": "$_id.product",
                "count": "$aantal2"},
            },
            "cnt": {"$sum" : "$aantal2"}
        }
    }
])

我希望得到这样的结果:

{
     Location: 25,
     sublocation: 01,
     <productTypeX> : <count the number of these products>,
     <productTypeY> : <count the number of these products>,
     <productTypeZ> : <count the number of these products>
},
{
     Location: 25,
     sublocation: 02,
     <productTypeX> : <count the number of these products>,
     <productTypeY> : <count the number of these products>,
     <productTypeZ> : <count the number of these products>
}

或:

{
    location: 25,
    sublocation: 01,
    products: [
         {
              <productTypeX> : <count>,
              <productTypeY> : <count>,
              <productTypeZ> : <count>
         }
},
{ 
    location: 25,
    sublocation: 02
    products: [
         {
              <productTypeX> : <count>,
              <productTypeY> : <count>,
              <productTypeZ> : <count>
         }
 }

如果我开始工作,我需要将它转换为mongoose for NodeJS(欢迎提示)

提前致谢!

0 个答案:

没有答案