У меня есть запрос, связанный с агрегацией mongodb - PullRequest
0 голосов
/ 08 октября 2019

Я новая пчела в структуре агрегации. Iam пытается сгенерировать счет.

Ниже приведены мои данные

{ "_id" : ObjectId("5d9726f21b1307ad58a237e8"), "invoice_month" : 10, "summarynumberprice" : { "local_number_minutes_price" : NumberDecimal("0.2000"), "byprefix_number_price" : NumberDecimal("2.0000"), "bystate_number_price" : NumberDecimal("1.0000"), "byprefix_minutes_price" : NumberDecimal("0.0100"), "bystate_minutes_price" : NumberDecimal("0.0200"), "local_number_price" : NumberDecimal("4.0000"), "toll_free_number_price" : NumberDecimal("1.0000"), "toll_free_minutes_price" : NumberDecimal("2.0000"), "tollfree_qnty" : 3, "bystate_quantity" : 0, "local_qnty" : 0, "byprefix_qnty" : 1 }, "agency_invoice_number" : "AG-194-000004", "agency_id" : 194, "summary" : { "By-State" : { "price" : NumberDecimal("0.0200"), "minutes" : 0 }, "Local" : { "price" : NumberDecimal("0.2000"), "minutes" : 0 }, "By-prefix" : { "price" : NumberDecimal("0.0100"), "minutes" : 0 }, "Toll-Free" : { "price" : NumberDecimal("2.0000"), "minutes" : 5 } }, "invoicemonth" : "2019-10", "agency_name" : "newWysecenter Agency", "isodate" : ISODate("2019-10-04T16:33:10.465Z"), "agency_address" : "test", "totalcallduration" : { "By-prefix" : 0, "Toll-Free" : 5 }, "date" : "2019-10-04", "isodateutc" : ISODate("2019-10-04T11:03:10.465Z"), "invoice_year" : 2019, "number_pricing" : { "By-prefix" : 0, "Toll-Free" : NumberDecimal("1.0000") } }

{ "_id" : ObjectId("5d97289b1b1307aed3341e38"), "invoice_month" : 10, "summarynumberprice" : { "local_number_minutes_price" : NumberDecimal("0.2000"), "byprefix_number_price" : NumberDecimal("2.0000"), "bystate_number_price" : NumberDecimal("1.0000"), "byprefix_minutes_price" : NumberDecimal("0.0100"), "bystate_minutes_price" : NumberDecimal("0.0200"), "local_number_price" : NumberDecimal("4.0000"), "toll_free_number_price" : NumberDecimal("1.0000"), "toll_free_minutes_price" : NumberDecimal("2.0000"), "tollfree_qnty" : 3, "bystate_quantity" : 0, "local_qnty" : 0, "byprefix_qnty" : 1 }, "agency_invoice_number" : "AG-194-000004", "agency_id" : 194, "summary" : { "By-State" : { "price" : NumberDecimal("0.0200"), "minutes" : 0 }, "Local" : { "price" : NumberDecimal("0.2000"), "minutes" : 0 }, "By-prefix" : { "price" : NumberDecimal("0.0100"), "minutes" : 0 }, "Toll-Free" : { "price" : NumberDecimal("2.0000"), "minutes" : 1 } }, "invoicemonth" : "2019-10", "agency_name" : "newWysecenter Agency", "isodate" : ISODate("2019-10-04T16:40:13.967Z"), "agency_address" : "test", "totalcallduration" : { "By-prefix" : 0, "Toll-Free" : 1 }, "date" : "2019-10-04", "isodateutc" : ISODate("2019-10-04T11:10:13.967Z"), "invoice_year" : 2019, "number_pricing" : { "By-prefix" : 0, "Toll-Free" : NumberDecimal("1.0000") } }

Совокупный запрос, Iam пытается создать следующий

db.getCollection('agency_invoice_daily_194').aggregate([
 {$unwind: '$summarynumberprice'},
 {$match:{ 'invoicemonth':'2019-10'}},

    {$group: {
        _id:'$invoicemonth',
       "name": { "$first": "$agency_name" },
       "agency_address": { "$first": "$agency_address" },
       "invoice_number": { "$first": "$agency_invoice_number" },
        prefix: {$addToSet : {number:'$summarynumberprice.byprefix_qnty' ,price:'$summarynumberprice.byprefix_number_price'  }},     
        state: {$addToSet : {number:'$summarynumberprice.bystate_quantity' ,price:'$summarynumberprice.bystate_number_price'  }},     
        local: {$addToSet : {number:'$summarynumberprice.local_qnty' ,price:'$summarynumberprice.local_number_price'  }},     
        tollfree: {$addToSet : {number:'$summarynumberprice.tollfree_qnty' ,price:'$summarynumberprice.toll_free_number_price'  }},  

        tollfreeminutes: {$addToSet : {price:'$summary.Toll-Free.price' , sum :{"$sum":{"$sum": '$summary.Toll-Free.minutes'}}  }}    

    }},
{
        "$project": {
            "_id": 0,  
               "agencyname": "$name","agency_name": 1, "agency_address": "$agency_address","invoice_number": "$invoice_number", "invoicemonth": "$_id","prefix": "$prefix","local": "$local" ,"state": "$state", "tollfree": "$tollfree",
               "tollfreeminutes": "$tollfreeminutes"

    }}



]).pretty();

Вывод запросаследующие

{
    "agencyname" : "newWysecenter Agency",
    "agency_address" : "test",
    "invoice_number" : "AG-194-000004",
    "invoicemonth" : "2019-10",
    "prefix" : [
        {
            "number" : 1,
            "price" : NumberDecimal("2.0000")
        }
    ],
    "local" : [
        {
            "number" : 0,
            "price" : NumberDecimal("4.0000")
        }
    ],
    "state" : [
        {
            "number" : 0,
            "price" : NumberDecimal("1.0000")
        }
    ],
    "tollfree" : [
        {
            "number" : 3,
            "price" : NumberDecimal("1.0000")
        }
    ],
    "tollfreeminutes" : [
        {
            "price" : NumberDecimal("2.0000"),
            "sum" : 1
        },
        {
            "price" : NumberDecimal("2.0000"),
            "sum" : 5
        }
    ]
}

Я хотел бы сгруппировать и добавить значения «суммы» ключей в течение бесплатных минут, имеющих одинаковую цену. Так что в результате массив tollfreeminutes становится

 "tollfreeminutes" : [
        {
            "price" : NumberDecimal("2.0000"),
            "sum" : 6
        }
    ] 

Надеюсь, что кто-томожет предоставить ценный запрос.

...