The aim of this query is when you are "Given an employee return a total sales number".
My first query was this
db.Employee.aggregate([{$lookup: {from: "Invoice", localField: "_id", foreignField: "_id", as: "Invoices"}}, {$match: {_id: 2}}]).pretty()
which returned the below snippet though it only returns one customer even though the employee has several customers. I'm not totally sure why it returns just one.
{
"_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
}
]
}
]
}
In an attempt to get around this and achieve my aim of returning the total sales number I created this new query
db.Employee.aggregate([{$unwind: "$_id"}, {$lookup: {from: "Invoice", localField: "_id", foreignField: "_id", as: "Invoices"}}, {$match: {_id: 2}}, {$group: {_id: "$_id", Total: {$sum: "$Total"}}}]).pretty()
though it just returns { "_id" : 2, "Total" : 0 }
Having looked at other issues I'm thinking it might be because the documents are nested though having tried the potential solution it has yielded no output. No errors which is good but nothing happens. Here is the query I tried:
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()
I don't understand why this query is not returning the total. Everything I've tried has failed. Any help is appreciated.
Edit:
My database is structured as so: Employee>Customer>Invoice. Customer references Employee via SupportRepId which is the same as an employee id as each customer is assigned an employee and Invoice contains the customer id as each invoice has a customer. So I want to get all invoices and sum there total based on an employee id.
Employee Example:
{
"_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"
}
Customer Example:
{
"_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
}
Invoice Example:
{
"_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
}
]
}