mongodb-$ sum返回0

时间:2019-02-11 19:58:08

标签: mongodb mongodb-query aggregation-framework

此查询的目的是当您“给员工返还销售总额”时。

我的第一个查询是

db.Employee.aggregate([{$lookup: {from: "Invoice", localField: "_id", foreignField: "_id", as: "Invoices"}}, {$match: {_id: 2}}]).pretty()

返回了以下代码段,尽管即使该员工有多个客户,它也只返回一个客户。我不太确定为什么它只返回一个。

{
"_id" : 2,
"LastName" : "Edwards",
"FirstName" : "Nancy",
"Title" : "Sales Manager",
"ReportsTo" : 1,
"BirthDate" : ISODate("1958-12-08T00:00:00Z"),
"HireDate" : ISODate("2002-05-01T00:00:00Z"),
"Address" : "825 8 Ave SW",
"City" : "Calgary",
"State" : "AB",
"Country" : "Canada",
"PostalCode" : "T2P 2T3",
"Phone" : "+1 (403) 262-3443",
"Fax" : "+1 (403) 262-3322",
"Email" : "nancy@chinookcorp.com",
"Invoices" : [
    {
        "_id" : 2,
        "CustomerId" : 4,
        "InvoiceDate" : ISODate("2009-01-02T00:00:00Z"),
        "BillingAddress" : "Ullevålsveien 14",
        "BillingCity" : "Oslo",
        "BillingState" : null,
        "BillingCountry" : "Norway",
        "BillingPostalCode" : "0171",
        "Total" : 3.96,
        "InvoiceLines" : [
            {
                "_id" : 3,
                "TrackId" : 6,
                "UnitPrice" : 0.99,
                "Quantity" : 1
            },
            {
                "_id" : 4,
                "TrackId" : 8,
                "UnitPrice" : 0.99,
                "Quantity" : 1
            },
            {
                "_id" : 5,
                "TrackId" : 10,
                "UnitPrice" : 0.99,
                "Quantity" : 1
            },
            {
                "_id" : 6,
                "TrackId" : 12,
                "UnitPrice" : 0.99,
                "Quantity" : 1
            }
        ]
    }
]
}

为了解决这个问题并实现返回总销售额的目的,我创建了这个新查询

db.Employee.aggregate([{$unwind: "$_id"}, {$lookup: {from: "Invoice", localField: "_id", foreignField: "_id", as: "Invoices"}}, {$match: {_id: 2}}, {$group: {_id: "$_id", Total: {$sum: "$Total"}}}]).pretty()

尽管它只返回{ "_id" : 2, "Total" : 0 }

看过其他问题,我想这可能是因为尽管尝试了潜在的解决方案但文档没有嵌套,但文档却是嵌套的。没有好的错误,但是什么也没有发生。这是我尝试过的查询:

db.Employee.aggregate([{$unwind: "$_id"}, {$unwind: "$_id.Invoices"}, {$unwind: "$_id.Invoices.InvoiceLines"}, {$lookup: {from: "Invoice", localField: "_id", foreignField: "_id", as: "Invoices"}}, {$match: {_id: 2}}, {$group: {_id: "$_id", Total: {$sum: "$Total"}}}]).pretty()

我不明白为什么这个查询没有返回总数。我尝试过的一切都失败了。任何帮助表示赞赏。

编辑:

我的数据库的结构如下:员工>客户>发票。客户通过SupportRepId引用Employee,这与员工ID相同,因为为每个客户分配了一名员工,并且由于每个发票都有一个客户,因此Invoice包含客户ID。因此,我想获取所有发票并根据员工ID汇总总和。

员工示例:

{
    "_id":3,
    "LastName":"Peacock",
    "FirstName":"Jane",
    "Title":"Sales Support Agent",
    "ReportsTo":2,
    "BirthDate":    ISODate("1973-08-29T00:00:00    Z"),
    "HireDate":    ISODate("2002-04-01T00:00:00    Z"),
    "Address":"1111 6 Ave SW",
    "City":"Calgary",
    "State":"AB",
    "Country":"Canada",
    "PostalCode":"T2P 5M5",
    "Phone":"+1 (403) 262-3443",
    "Fax":"+1 (403) 262-6712",
    "Email":"jane@chinookcorp.com"
}

客户示例:

{
    "_id":1,
    "FirstName":"Luís",
    "LastName":"Gonçalves",
    "Company":"Embraer - Empresa Brasileira de Aeronáutica S.A.",
    "Address":"Av. Brigadeiro Faria Lima, 2170",
    "City":"São José dos Campos",
    "State":"SP",
    "Country":"Brazil",
    "PostalCode":"12227-000",
    "Phone":"+55 (12) 3923-5555",
    "Fax":"+55 (12) 3923-5566",
    "Email":"luisg@embraer.com.br",
    "SupportRepId":3
}

发票示例:

{
    "_id":2,
    "CustomerId":4,
    "InvoiceDate":    ISODate("2009-01-02T00:00:00    Z"),
    "BillingAddress":"Ullevålsveien 14",
    "BillingCity":"Oslo",
    "BillingState":null,
    "BillingCountry":"Norway",
    "BillingPostalCode":"0171",
    "Total":3.96,
    "InvoiceLines":[
        {
            "_id":3,
            "TrackId":6,
            "UnitPrice":0.99,
            "Quantity":1
        },
        {
            "_id":4,
            "TrackId":8,
            "UnitPrice":0.99,
            "Quantity":1
        },
        {
            "_id":5,
            "TrackId":10,
            "UnitPrice":0.99,
            "Quantity":1
        },
        {
            "_id":6,
            "TrackId":12,
            "UnitPrice":0.99,
            "Quantity":1
        }
    ]
}

1 个答案:

答案 0 :(得分:0)

看看是否可行

$('#myformid').on('beforeSubmit', function (e) {
    e.preventDefault();

    var form = $(this);
    var formData = form.serializeArray();

    $.ajax({
        url: form.attr("action"),
        method: form.attr("method"),
        data: formData,
        success: function (data) {
            if (data !== false) {
                $('#modal-add-associate').modal('hide');
            }
        },
        error: function (jqXHR, textStatus, errorThrown) {
            console.log(jqXHR, textStatus, errorThrown);
        }
    });
    return false;
});