为缺少的分组数据生成默认值

时间:2019-05-20 03:53:19

标签: mongodb aggregation-framework

我正在尝试使用mongodb聚合框架来聚合数据。我希望结果始终包含我在第2阶段中匹配的所有类别,即使没有针对它们的数据。缺少的数据应显示为0:与我当前的输出相比,我缺少

{
   period: 05/2019, category: 1, subtotal: 0,
   period: 04/2019, category: 1, subtotal: 0,
   period: 03/2019, category: 3, subtotal: 0,
   ...
}

我具有以下数据结构:

{ "_id" : { "$oid" : "5c0f8703925acc3248202baa" }, "date" : 1.5436152E12, "period" : "12/2018", "category" : 0, "subcategory" : 0, "amount" : 151.24 }
{ "_id" : { "$oid" : "5c0f876c925acc3248202bac" }, "date" : 1.54422E12, "period" : "12/2018", "category" : 0, "subcategory" : 0, "amount" : 62.67 }
{ "_id" : { "$oid" : "5c0f8796925acc3248202bad" }, "date" : 1.5418008E12, "period" : "11/2018", "category" : 0, "subcategory" : 0, "amount" : 6.17 }
{ "_id" : { "$oid" : "5c0f87c3925acc3248202bae" }, "date" : 1.5418008E12, "period" : "11/2018", "category" : 0, "subcategory" : 0, "amount" : 17.86 }
{ "_id" : { "$oid" : "5c0f87e1925acc3248202baf" }, "date" : 1.5424056E12, "period" : "11/2018", "category" : 0, "subcategory" : 0, "amount" : 47.73 }
{ "_id" : { "$oid" : "5c0f87f6925acc3248202bb0" }, "date" : 1.5430104E12, "period" : "11/2018", "category" : 0, "subcategory" : 0, "amount" : 30.22 }
{ "_id" : { "$oid" : "5c0f8815925acc3248202bb1" }, "date" : 1.5424056E12, "period" : "11/2018", "category" : 0, "subcategory" : 0, "amount" : 69.58 }
{ "_id" : { "$oid" : "5c0f88a5925acc3248202bb2" }, "date" : 1.5412824E12, "period" : "11/2018", "category" : 0, "subcategory" : 0, "amount" : 1.49 }
{ "_id" : { "$oid" : "5c0f88bc925acc3248202bb3" }, "date" : 1.541196E12, "period" : "11/2018", "category" : 0, "subcategory" : 0, "amount" : 89.81 }
{ "_id" : { "$oid" : "5c0f890a925acc3248202bb4" }, "date" : 1.5435288E12, "period" : "11/2018", "category" : 0, "subcategory" : 0, "amount" : 81.58 }
{ "_id" : { "$oid" : "5c0f892c925acc3248202bb5" }, "date" : 1.54422E12, "period" : "12/2018", "category" : 0, "subcategory" : 0, "amount" : 2.69 }
{ "_id" : { "$oid" : "5c0f8958925acc3248202bb6" }, "date" : 1.54422E12, "period" : "12/2018", "category" : 4, "subcategory" : 23, "amount" : 439 }
{ "_id" : { "$oid" : "5c0f89f8925acc3248202bb8" }, "date" : 1.541196E12, "period" : "11/2018", "category" : 0, "subcategory" : 0, "amount" : 10.34 }
{ "_id" : { "$oid" : "5c0f8a69925acc3248202bb9" }, "date" : 1.5430968E12, "period" : "11/2018", "category" : 2, "subcategory" : 6, "amount" : 42 }
{ "_id" : { "$oid" : "5c0f8b2b925acc3248202bba" }, "date" : 1.5411096E12, "period" : "11/2018", "category" : 0, "subcategory" : 0, "amount" : 39.39 }
{ "_id" : { "$oid" : "5c0f8b83925acc3248202bbb" }, "date" : 1.5410232E12, "period" : "11/2018", "category" : 5, "subcategory" : 20, "amount" : 14.99 }
{ "_id" : { "$oid" : "5c0f8ba9925acc3248202bbc" }, "date" : 1.5426648E12, "period" : "11/2018", "category" : 1, "subcategory" : 1, "amount" : 61.54 }
{ "_id" : { "$oid" : "5c0f8d39925acc3248202bbd" }, "date" : 1.5435288E12, "period" : "11/2018", "category" : 2, "subcategory" : 5, "amount" : 50 }
{ "_id" : { "$oid" : "5c0f8d53925acc3248202bbe" }, "date" : 1.5435288E12, "period" : "11/2018", "category" : 4, "subcategory" : 15, "amount" : 30 }
{ "_id" : { "$oid" : "5c0f8d62925acc3248202bbf" }, "date" : 1.5435288E12, "period" : "11/2018", "category" : 3, "subcategory" : 8, "amount" : 12 }
{ "_id" : { "$oid" : "5c0f8d90925acc3248202bc0" }, "date" : 1.5410232E12, "period" : "11/2018", "category" : 0, "subcategory" : 0, "amount" : 9 }
{ "_id" : { "$oid" : "5c0f8da2925acc3248202bc1" }, "date" : 1.5435288E12, "period" : "11/2018", "category" : 5, "subcategory" : 21, "amount" : 19.57 }
{ "_id" : { "$oid" : "5c0f8f8b925acc3248202bc2" }, "date" : 1.54422E12, "period" : "12/2018", "category" : 4, "subcategory" : 15, "amount" : 30 }
{ "_id" : { "$oid" : "5c0f9029925acc3248202bc3" }, "date" : 1.5435288E12, "period" : "11/2018", "category" : 3, "subcategory" : 10, "amount" : 50 }
{ "_id" : { "$oid" : "5c0f90a9925acc3248202bc4" }, "date" : 1.5443064E12, "period" : "12/2018", "category" : 0, "subcategory" : 0, "amount" : 3.17 }
{ "_id" : { "$oid" : "5c0f90e2925acc3248202bc5" }, "date" : 1.5411096E12, "period" : "11/2018", "category" : 0, "subcategory" : 0, "amount" : 10.27 }
{ "_id" : { "$oid" : "5c0f9264925acc3248202bc6" }, "date" : 1.5422328E12, "period" : "11/2018", "category" : 4, "subcategory" : 23, "amount" : 7.93 }
{ "_id" : { "$oid" : "5c0f9293925acc3248202bc7" }, "date" : 1.5418008E12, "period" : "11/2018", "category" : 0, "subcategory" : 0, "amount" : 9.78 }
{ "_id" : { "$oid" : "5c0f9936925acc3248202bc8" }, "date" : 1.5441336E12, "period" : "12/2018", "category" : 4, "subcategory" : 23, "amount" : 145.2 }
{ "_id" : { "$oid" : "5c10d422925acc3248202bcc" }, "date" : 1.5430104E12, "period" : "11/2018", "category" : 0, "subcategory" : 0, "amount" : 54.5 }
{ "_id" : { "$oid" : "5c10d450925acc3248202bcd" }, "date" : 1.54422E12, "period" : "12/2018", "category" : 0, "subcategory" : 0, "amount" : 9.57 }
{ "_id" : { "$oid" : "5c3eef1d974e461778360bab" }, "date" : 1.5473304E12, "period" : "01/2019", "category" : 0, "subcategory" : 0, "amount" : 6.69 }
{ "_id" : { "$oid" : "5c3eef76974e461778360bac" }, "date" : 1.5473304E12, "period" : "01/2019", "category" : 0, "subcategory" : 0, "amount" : 5.49 }
{ "_id" : { "$oid" : "5c3ef002974e461778360bad" }, "date" : 1.547244E12, "period" : "01/2019", "category" : 2, "subcategory" : 3, "amount" : 37.97 }
{ "_id" : { "$oid" : "5c3ef051974e461778360bae" }, "date" : 1.547244E12, "period" : "01/2019", "category" : 2, "subcategory" : 5, "amount" : 5.78 }
{ "_id" : { "$oid" : "5c3ef0b7974e461778360baf" }, "date" : 1.547244E12, "period" : "01/2019", "category" : 0, "subcategory" : 0, "amount" : 117.43 }
{ "_id" : { "$oid" : "5c3ef13c974e461778360bb0" }, "date" : 1.547244E12, "period" : "01/2019", "category" : 0, "subcategory" : 0, "amount" : 19.51 }
{ "_id" : { "$oid" : "5c3ef2ee974e461778360bb2" }, "date" : 1.547244E12, "period" : "01/2019", "category" : 0, "subcategory" : 0, "amount" : 4.4 }
{ "_id" : { "$oid" : "5c3ef3ff974e461778360bb4" }, "date" : 1.5457752E12, "period" : "12/2018", "category" : 0, "subcategory" : 0, "amount" : 1.69 }
{ "_id" : { "$oid" : "5c3ef4a1974e461778360bb5" }, "date" : 1.5466392E12, "period" : "01/2019", "category" : 2, "subcategory" : 3, "amount" : 6.6 }
{ "_id" : { "$oid" : "5c3ef4ed974e461778360bb6" }, "date" : 1.5460344E12, "period" : "12/2018", "category" : 4, "subcategory" : 23, "amount" : 11.7 }
{ "_id" : { "$oid" : "5c3ef579974e461778360bb9" }, "date" : 1.5451704E12, "period" : "12/2018", "category" : 5, "subcategory" : 20, "amount" : 14.99 }
{ "_id" : { "$oid" : "5c3ef641974e461778360bba" }, "date" : 1.5458616E12, "period" : "12/2018", "category" : 4, "subcategory" : 23, "amount" : 69.99 }
{ "_id" : { "$oid" : "5c3ef791974e461778360bbb" }, "date" : 1.5462072E12, "period" : "12/2018", "category" : 0, "subcategory" : 0, "amount" : 2.9 }
{ "_id" : { "$oid" : "5c3ef836974e461778360bbc" }, "date" : 1.5448248E12, "period" : "12/2018", "category" : 0, "subcategory" : 0, "amount" : 30.76 }
{ "_id" : { "$oid" : "5c3efb0e974e461778360bbe" }, "date" : 1.5460344E12, "period" : "12/2018", "category" : 4, "subcategory" : 23, "amount" : 3.99 }
{ "_id" : { "$oid" : "5c3efb9e974e461778360bbf" }, "date" : 1.545516E12, "period" : "12/2018", "category" : 2, "subcategory" : 6, "amount" : 8.1 }
{ "_id" : { "$oid" : "5c3efd46974e461778360bc0" }, "date" : 1.545516E12, "period" : "12/2018", "category" : 2, "subcategory" : 3, "amount" : 10 }
{ "_id" : { "$oid" : "5c3efd93974e461778360bc1" }, "date" : 1.5466392E12, "period" : "01/2019", "category" : 2, "subcategory" : 6, "amount" : 25 }
{ "_id" : { "$oid" : "5c3effc1974e461778360bc3" }, "date" : 1.5473304E12, "period" : "01/2019", "category" : 2, "subcategory" : 6, "amount" : 9.5 }
{ "_id" : { "$oid" : "5c3effef974e461778360bc4" }, "date" : 1.5473304E12, "period" : "01/2019", "category" : 4, "subcategory" : 15, "amount" : 30 }
{ "_id" : { "$oid" : "5c3f000c974e461778360bc5" }, "date" : 1.5473304E12, "period" : "01/2019", "category" : 5, "subcategory" : 21, "amount" : 19.57 }
{ "_id" : { "$oid" : "5c3f0017974e461778360bc6" }, "date" : 1.5461208E12, "period" : "12/2018", "category" : 5, "subcategory" : 21, "amount" : 19.57 }
{ "_id" : { "$oid" : "5c3f003f974e461778360bc7" }, "date" : 1.5449976E12, "period" : "12/2018", "category" : 4, "subcategory" : 23, "amount" : 9.42 }
{ "_id" : { "$oid" : "5c3f009a974e461778360bc9" }, "date" : 1.5452568E12, "period" : "12/2018", "category" : 1, "subcategory" : 1, "amount" : 132.44 }
{ "_id" : { "$oid" : "5c3f00ec974e461778360bca" }, "date" : 1.5452568E12, "period" : "12/2018", "category" : 0, "subcategory" : 0, "amount" : 6.54 }
{ "_id" : { "$oid" : "5c3f010d974e461778360bcb" }, "date" : 1.545516E12, "period" : "12/2018", "category" : 0, "subcategory" : 0, "amount" : 24.42 }
{ "_id" : { "$oid" : "5c3f0127974e461778360bcc" }, "date" : 1.544652E12, "period" : "12/2018", "category" : 0, "subcategory" : 0, "amount" : 3.56 }
{ "_id" : { "$oid" : "5c3f0142974e461778360bcd" }, "date" : 1.5436152E12, "period" : "12/2018", "category" : 0, "subcategory" : 0, "amount" : 2.28 }
{ "_id" : { "$oid" : "5c3f015b974e461778360bce" }, "date" : 1.5452568E12, "period" : "12/2018", "category" : 0, "subcategory" : 0, "amount" : 4.88 }
{ "_id" : { "$oid" : "5c3f0191974e461778360bcf" }, "date" : 1.5448248E12, "period" : "12/2018", "category" : 0, "subcategory" : 0, "amount" : 6.49 }
{ "_id" : { "$oid" : "5c3f01b9974e461778360bd0" }, "date" : 1.5449976E12, "period" : "12/2018", "category" : 0, "subcategory" : 0, "amount" : 2.23 }
{ "_id" : { "$oid" : "5c3f06e3974e461778360bd2" }, "date" : 1.5447384E12, "period" : "12/2018", "category" : 2, "subcategory" : 3, "amount" : 22 }
{ "_id" : { "$oid" : "5c3f06ef974e461778360bd3" }, "date" : 1.5447384E12, "period" : "12/2018", "category" : 2, "subcategory" : 3, "amount" : 8 }
{ "_id" : { "$oid" : "5c3f070e974e461778360bd4" }, "date" : 1.5447384E12, "period" : "12/2018", "category" : 2, "subcategory" : 3, "amount" : 8 }
{ "_id" : { "$oid" : "5c3f0730974e461778360bd5" }, "date" : 1.544652E12, "period" : "12/2018", "category" : 4, "subcategory" : 23, "amount" : 28 }
{ "_id" : { "$oid" : "5c3f0762974e461778360bd6" }, "date" : 1.5447384E12, "period" : "12/2018", "category" : 6, "subcategory" : 22, "amount" : 50 }
{ "_id" : { "$oid" : "5c3f0979974e461778360bd7" }, "date" : 1.5467256E12, "period" : "01/2019", "category" : 0, "subcategory" : 0, "amount" : 4.09 }
{ "_id" : { "$oid" : "5c654c3caaa31d420c4cbf3d" }, "date" : 1.5480216E12, "period" : "01/2019", "category" : 5, "subcategory" : 20, "amount" : 14.99 }
{ "_id" : { "$oid" : "5c654c67aaa31d420c4cbf3e" }, "date" : 1.5483672E12, "period" : "01/2019", "category" : 1, "subcategory" : 1, "amount" : 161.81 }
{ "_id" : { "$oid" : "5c654cd8aaa31d420c4cbf40" }, "date" : 1.5475896E12, "period" : "01/2019", "category" : 2, "subcategory" : 3, "amount" : 36 }
{ "_id" : { "$oid" : "5c654cedaaa31d420c4cbf41" }, "date" : 1.5475896E12, "period" : "01/2019", "category" : 3, "subcategory" : 10, "amount" : 50 }
{ "_id" : { "$oid" : "5c654d09aaa31d420c4cbf42" }, "date" : 1.5475896E12, "period" : "01/2019", "category" : 2, "subcategory" : 3, "amount" : 6 }
{ "_id" : { "$oid" : "5c654d45aaa31d420c4cbf43" }, "date" : 1.5490584E12, "period" : "02/2019", "category" : 0, "subcategory" : 0, "amount" : 20.67 }
{ "_id" : { "$oid" : "5c654d89aaa31d420c4cbf44" }, "date" : 1.5477624E12, "period" : "01/2019", "category" : 0, "subcategory" : 0, "amount" : 12.68 }
{ "_id" : { "$oid" : "5c654da6aaa31d420c4cbf45" }, "date" : 1.5490584E12, "period" : "02/2019", "category" : 0, "subcategory" : 0, "amount" : 12.09 }
{ "_id" : { "$oid" : "5c654dd1aaa31d420c4cbf46" }, "date" : 1.5490584E12, "period" : "02/2019", "category" : 0, "subcategory" : 0, "amount" : 92.19 }
{ "_id" : { "$oid" : "5c654dffaaa31d420c4cbf47" }, "date" : 1.5481944E12, "period" : "01/2019", "category" : 0, "subcategory" : 0, "amount" : 8.64 }
{ "_id" : { "$oid" : "5c654e1faaa31d420c4cbf48" }, "date" : 1.5484536E12, "period" : "01/2019", "category" : 0, "subcategory" : 0, "amount" : 12.24 }
{ "_id" : { "$oid" : "5c654e4eaaa31d420c4cbf49" }, "date" : 1.5496632E12, "period" : "02/2019", "category" : 0, "subcategory" : 0, "amount" : 76.78 }
{ "_id" : { "$oid" : "5c654e86aaa31d420c4cbf4a" }, "date" : 1.5484536E12, "period" : "01/2019", "category" : 0, "subcategory" : 0, "amount" : 57.17 }
{ "_id" : { "$oid" : "5c654eb0aaa31d420c4cbf4b" }, "date" : 1.5477624E12, "period" : "01/2019", "category" : 0, "subcategory" : 0, "amount" : 26.72 }
{ "_id" : { "$oid" : "5c655059aaa31d420c4cbf4c" }, "date" : 1.54854E12, "period" : "01/2019", "category" : 4, "subcategory" : 15, "amount" : 30 }
{ "_id" : { "$oid" : "5c655070aaa31d420c4cbf4d" }, "date" : 1.5497496E12, "period" : "02/2019", "category" : 4, "subcategory" : 15, "amount" : 30 }
{ "_id" : { "$oid" : "5c655096aaa31d420c4cbf4e" }, "date" : 1.5497496E12, "period" : "02/2019", "category" : 0, "subcategory" : 0, "amount" : 5 }
{ "_id" : { "$oid" : "5c6550b9aaa31d420c4cbf4f" }, "date" : 1.5500088E12, "period" : "02/2019", "category" : 2, "subcategory" : 6, "amount" : 50 }
{ "_id" : { "$oid" : "5c6550d0aaa31d420c4cbf50" }, "date" : 1.5500088E12, "period" : "02/2019", "category" : 2, "subcategory" : 6, "amount" : 7 }
{ "_id" : { "$oid" : "5c655145aaa31d420c4cbf51" }, "date" : 1.5477624E12, "period" : "01/2019", "category" : 0, "subcategory" : 0, "amount" : 120.92 }
{ "_id" : { "$oid" : "5c72c90673bf83285c26f5a7" }, "date" : 1.5507864E12, "period" : "02/2019", "category" : 5, "subcategory" : 20, "amount" : 19.98 }
{ "_id" : { "$oid" : "5c72c91b73bf83285c26f5a8" }, "date" : 1.5509592E12, "period" : "02/2019", "category" : 4, "subcategory" : 15, "amount" : 30 }
{ "_id" : { "$oid" : "5c72c97d73bf83285c26f5a9" }, "date" : 1.550268E12, "period" : "02/2019", "category" : 0, "subcategory" : 0, "amount" : 26.31 }
{ "_id" : { "$oid" : "5c72c9b373bf83285c26f5aa" }, "date" : 1.5505272E12, "period" : "02/2019", "category" : 0, "subcategory" : 0, "amount" : 7.51 }
{ "_id" : { "$oid" : "5c72cf1373bf83285c26f5ac" }, "date" : 1.5509592E12, "period" : "02/2019", "category" : 1, "subcategory" : 1, "amount" : 129.44 }
{ "_id" : { "$oid" : "5c73faf9489e834e8c4a78b2" }, "date" : 1.5510456E12, "period" : "02/2019", "category" : 0, "subcategory" : 0, "amount" : 6.96 }
{ "_id" : { "$oid" : "5c73fb0e489e834e8c4a78b3" }, "date" : 1.5510456E12, "period" : "02/2019", "category" : 0, "subcategory" : 0, "amount" : 44.87 }
{ "_id" : { "$oid" : "5c7faea4ccc2d34ec4d86be8" }, "date" : 1.5512184E12, "period" : "02/2019", "category" : 4, "subcategory" : 19, "amount" : 293.58 }
{ "_id" : { "$oid" : "5c7faeddccc2d34ec4d86be9" }, "date" : 1.5512184E12, "period" : "02/2019", "category" : 3, "subcategory" : 14, "amount" : 31.35 }
{ "_id" : { "$oid" : "5c7fafdbccc2d34ec4d86bea" }, "date" : 1.5514776E12, "period" : "03/2019", "category" : 2, "subcategory" : 7, "amount" : 180 }
{ "_id" : { "$oid" : "5c7fb008ccc2d34ec4d86beb" }, "date" : 1.5514776E12, "period" : "03/2019", "category" : 2, "subcategory" : 7, "amount" : 23 }
{ "_id" : { "$oid" : "5c7fb01cccc2d34ec4d86bec" }, "date" : 1.5514776E12, "period" : "03/2019", "category" : 2, "subcategory" : 7, "amount" : 2 }
{ "_id" : { "$oid" : "5c7fb258ccc2d34ec4d86bed" }, "date" : 1.5506136E12, "period" : "02/2019", "category" : 5, "subcategory" : 21, "amount" : 23.48 }
{ "_id" : { "$oid" : "5c7fb5cdccc2d34ec4d86bef" }, "date" : 1.5530328E12, "period" : "03/2019", "category" : 5, "subcategory" : 21, "amount" : 23.48 }
{ "_id" : { "$oid" : "5c838d521927dc0ed8634bca" }, "date" : 1.5520824E12, "period" : "03/2019", "category" : 0, "subcategory" : 0, "amount" : 16.25 }
{ "_id" : { "$oid" : "5c838d621927dc0ed8634bcb" }, "date" : 1.5520824E12, "period" : "03/2019", "category" : 0, "subcategory" : 0, "amount" : 79 }
{ "_id" : { "$oid" : "5ca48f9b0e04454b0c7a3a69" }, "date" : 1.5538968E12, "period" : "03/2019", "category" : 0, "subcategory" : 0, "amount" : 6.44 }
{ "_id" : { "$oid" : "5ca490900e04454b0c7a3a70" }, "date" : 1.553292E12, "period" : "03/2019", "category" : 2, "subcategory" : 3, "amount" : 6.2 }
{ "_id" : { "$oid" : "5ca4915c0e04454b0c7a3a78" }, "date" : 1.553292E12, "period" : "03/2019", "category" : 0, "subcategory" : 0, "amount" : 4.3 }
{ "_id" : { "$oid" : "5ca492780e04454b0c7a3a86" }, "date" : 1.5538968E12, "period" : "03/2019", "category" : 0, "subcategory" : 0, "amount" : 56.6 }
{ "_id" : { "$oid" : "5ca493850e04454b0c7a3aa0" }, "date" : 1.5526872E12, "period" : "03/2019", "category" : 0, "subcategory" : 0, "amount" : 3.43 }
{ "_id" : { "$oid" : "5ca4940f0e04454b0c7a3aa8" }, "date" : 1.5520824E12, "period" : "03/2019", "category" : 0, "subcategory" : 0, "amount" : 17.05 }
{ "_id" : { "$oid" : "5ca495340e04454b0c7a3aaf" }, "date" : 1.553292E12, "period" : "03/2019", "category" : 0, "subcategory" : 0, "amount" : 50.99 }
{ "_id" : { "$oid" : "5ca495980e04454b0c7a3ab6" }, "date" : 1.5526008E12, "period" : "03/2019", "category" : 0, "subcategory" : 0, "amount" : 4.99 }
{ "_id" : { "$oid" : "5ca496310e04454b0c7a3abd" }, "date" : 1.553292E12, "period" : "03/2019", "category" : 0, "subcategory" : 0, "amount" : 5.28 }
{ "_id" : { "$oid" : "5ca4985d0e04454b0c7a3aca" }, "date" : 1.5526872E12, "period" : "03/2019", "category" : 0, "subcategory" : 0, "amount" : 3.08 }
{ "_id" : { "$oid" : "5ca498db0e04454b0c7a3ad7" }, "date" : 1.5526872E12, "period" : "03/2019", "category" : 0, "subcategory" : 0, "amount" : 30.12 }
{ "_id" : { "$oid" : "5ca4995e0e04454b0c7a3ade" }, "date" : 1.5526872E12, "period" : "03/2019", "category" : 0, "subcategory" : 0, "amount" : 3.5 }
{ "_id" : { "$oid" : "5ca499f10e04454b0c7a3aec" }, "date" : 1.5534648E12, "period" : "03/2019", "category" : 1, "subcategory" : 1, "amount" : 92.84 }
{ "_id" : { "$oid" : "5ca49a350e04454b0c7a3aed" }, "date" : 1.5534648E12, "period" : "03/2019", "category" : 5, "subcategory" : 20, "amount" : 9.42 }
{ "_id" : { "$oid" : "5ca49aa10e04454b0c7a3aee" }, "date" : 1.5523416E12, "period" : "03/2019", "category" : 0, "subcategory" : 0, "amount" : 6.7 }
{ "_id" : { "$oid" : "5ca49b430e04454b0c7a3af5" }, "date" : 1.5523416E12, "period" : "03/2019", "category" : 0, "subcategory" : 0, "amount" : 4.63 }
{ "_id" : { "$oid" : "5ca49ce20e04454b0c7a3afc" }, "date" : 1.5539832E12, "period" : "03/2019", "category" : 4, "subcategory" : 15, "amount" : 30 }
{ "_id" : { "$oid" : "5ca49d1b0e04454b0c7a3afd" }, "date" : 1.5526872E12, "period" : "03/2019", "category" : 4, "subcategory" : 15, "amount" : 20 }
{ "_id" : { "$oid" : "5ca49d360e04454b0c7a3afe" }, "date" : 1.5526872E12, "period" : "03/2019", "category" : 4, "subcategory" : 15, "amount" : 10 }
{ "_id" : { "$oid" : "5ca49d830e04454b0c7a3aff" }, "date" : 1.5523416E12, "period" : "03/2019", "category" : 0, "subcategory" : 0, "amount" : 2.95 }
{ "_id" : { "$oid" : "5cb1a258ea5eac25e84d0baa" }, "date" : 1.554498E12, "period" : "04/2019", "category" : 0, "subcategory" : 0, "amount" : 7.65 }
{ "_id" : { "$oid" : "5cb1a30cea5eac25e84d0bb2" }, "date" : 1.5547572E12, "period" : "04/2019", "category" : 3, "subcategory" : 13, "amount" : 50 }
{ "_id" : { "$oid" : "5cb1a393ea5eac25e84d0bb4" }, "date" : 1.5548436E12, "period" : "04/2019", "category" : 2, "subcategory" : 6, "amount" : 30 }
{ "_id" : { "$oid" : "5cb1a3faea5eac25e84d0bb5" }, "date" : 1.5551028E12, "period" : "04/2019", "category" : 0, "subcategory" : 0, "amount" : 4.01 }
{ "_id" : { "$oid" : "5cb1a439ea5eac25e84d0bbc" }, "date" : 1.5551028E12, "period" : "04/2019", "category" : 0, "subcategory" : 0, "amount" : 7.45 }
{ "_id" : { "$oid" : "5cb1a5c61d4248037cebd00d" }, "date" : 1.554498E12, "period" : "04/2019", "category" : 0, "subcategory" : 0, "amount" : 4.78 }
{ "_id" : { "$oid" : "5cb1a6931d4248037cebd021" }, "date" : 1.5551028E12, "period" : "04/2019", "category" : 0, "subcategory" : 0, "amount" : 48.23 }
{ "_id" : { "$oid" : "5cb1a7771d4248037cebd037" }, "date" : 1.5551028E12, "period" : "04/2019", "category" : 0, "subcategory" : 0, "amount" : 7.38 }
{ "_id" : { "$oid" : "5cb1a7e71d4248037cebd04b" }, "date" : 1.554498E12, "period" : "04/2019", "category" : 0, "subcategory" : 0, "amount" : 56.71 }
{ "_id" : { "$oid" : "5cbd75d7fdaebb35845fc7f9" }, "date" : 1.5556212E12, "period" : "04/2019", "category" : 5, "subcategory" : 20, "amount" : 11.99 }
{ "_id" : { "$oid" : "5ccc291fbb8fa12010befcfa" }, "date" : 1.5567444E12, "period" : "05/2019", "category" : 0, "subcategory" : 0, "amount" : 12.22 }
{ "_id" : { "$oid" : "5cd12b1d9173a235509cfd65" }, "date" : 1.556658E12, "period" : "05/2019", "category" : 3, "subcategory" : 14, "amount" : 97 }
{ "_id" : { "$oid" : "5cd12bf89173a235509cfd66" }, "date" : 1.5565716E12, "period" : "04/2019", "category" : 0, "subcategory" : 0, "amount" : 6.97 }
{ "_id" : { "$oid" : "5cd12d389173a235509cfd89" }, "date" : 1.5561396E12, "period" : "04/2019", "category" : 6, "subcategory" : 22, "amount" : 64.33 }
{ "_id" : { "$oid" : "5cd12dcc9173a235509cfd99" }, "date" : 1.5565716E12, "period" : "04/2019", "category" : 2, "subcategory" : 3, "amount" : 4.99 }
{ "_id" : { "$oid" : "5cd12ed59173a235509cfdac" }, "date" : 1.5557076E12, "period" : "04/2019", "category" : 1, "subcategory" : 1, "amount" : 72.58 }
{ "_id" : { "$oid" : "5cd12f529173a235509cfdad" }, "date" : 1.5569172E12, "period" : "05/2019", "category" : 3, "subcategory" : 11, "amount" : 177 }
{ "_id" : { "$oid" : "5cd12f719173a235509cfdae" }, "date" : 1.5569172E12, "period" : "05/2019", "category" : 3, "subcategory" : 10, "amount" : 89 }
{ "_id" : { "$oid" : "5cd133cb67436428a4f64704" }, "date" : 1.5567444E12, "period" : "05/2019", "category" : 7, "subcategory" : 24, "amount" : 16.73 }
{ "_id" : { "$oid" : "5cd133fe67436428a4f64705" }, "date" : 1.5565716E12, "period" : "04/2019", "category" : 2, "subcategory" : 25, "amount" : 12 }
{ "_id" : { "$oid" : "5cd13593b9d4d21da0df0c49" }, "date" : 1.5568308E12, "period" : "05/2019", "category" : 5, "subcategory" : 26, "amount" : 10 }
{ "_id" : { "$oid" : "5cd13680b9d4d21da0df0c7e" }, "date" : 1.5568308E12, "period" : "05/2019", "category" : 5, "subcategory" : 26, "amount" : 17.8 }
{ "_id" : { "$oid" : "5cdfc508280322379425f20d" }, "date" : 1.5578676E12, "period" : "05/2019", "category" : 5, "subcategory" : 26, "amount" : 9 }
{ "_id" : { "$oid" : "5cdfc536280322379425f20e" }, "date" : 1.5578676E12, "period" : "05/2019", "category" : 0, "subcategory" : 0, "amount" : 34.24 }
{ "_id" : { "$oid" : "5cdfc559280322379425f20f" }, "date" : 1.5571764E12, "period" : "05/2019", "category" : 0, "subcategory" : 0, "amount" : 30.28 }
{ "_id" : { "$oid" : "5cdfc590280322379425f210" }, "date" : 1.5572628E12, "period" : "05/2019", "category" : 5, "subcategory" : 26, "amount" : 40 }
{ "_id" : { "$oid" : "5cdfc5b2280322379425f211" }, "date" : 1.5572628E12, "period" : "05/2019", "category" : 2, "subcategory" : 3, "amount" : 4.8 }
{ "_id" : { "$oid" : "5cdfc5c9280322379425f212" }, "date" : 1.5572628E12, "period" : "05/2019", "category" : 2, "subcategory" : 3, "amount" : 5 }
{ "_id" : { "$oid" : "5cdfc5f0280322379425f213" }, "date" : 1.5580404E12, "period" : "05/2019", "category" : 3, "subcategory" : 10, "amount" : 50 }
{ "_id" : { "$oid" : "5cdfc630280322379425f215" }, "date" : 1.5580404E12, "period" : "05/2019", "category" : 5, "subcategory" : 26, "amount" : 50 }

我建立了一个聚合管道,该管道可以正确生成每个期间/类别对的小计:

  db.getCollection("exps").aggregate(
        [
            // Stage 1
            {
                $project: {
                  period: 1, data:1, category:1, amount:1 
                }
            },

            // Stage 2  -- incoming categories from api call (example)
            {
                $match: {
                   category: { $in: [1,3] },
                },
            },

            // Stage 3
            {
                $group: {
                  _id: { 
                         period: "$period", 
                         category: "$category"
                       }, 
                       subtotal: {$sum:'$amount'}                   
                }
            },
        ]       
    );

输出

enter image description here

我用$ ifNull尝试过,$ exists,...但是似乎没有得到结果。如果可能的话,我想在Mongodb中完成大多数数据工作,但是如果我想对期间/类别对做同样的事情,也许可能就不可能了(角落情况:如果没有,我怎么知道缺少的期间出现其中的类别...)由于输出用于制图,因此我需要人为地生成缺失期间。

我正在考虑下一步尝试使用addToSet进行某种操作,然后进行某种查找吗?这是前进的方向吗?

更新1:(来自Mickl的反馈)

db.getCollection("exps").aggregate(

    // Pipeline
    [
        // Stage 1
        {
            $project: {
              period: 1, data:1, category:1, amount:1 
            }
        },

        // Stage 3
        {
            $group: {
              _id: { 
                 period: "$period",
                 category: "$category"
               }, 
               subtotal: {
                  $sum: { $cond: [ { $in: [ "$category", [1, 3] ] }, "$amount", 0 ] }
               } 
            }
        },

    ]


);

结果

{ "_id" : { "period" : "05/2019", "category" : 2 }, "subtotal" : 0.0 }
{ "_id" : { "period" : "04/2019", "category" : 6 }, "subtotal" : 0.0 }
{ "_id" : { "period" : "05/2019", "category" : 3 }, "subtotal" : 413 }
{ "_id" : { "period" : "05/2019", "category" : 5 }, "subtotal" : 0.0 }
{ "_id" : { "period" : "05/2019", "category" : 0 }, "subtotal" : 0.0 }
{ "_id" : { "period" : "04/2019", "category" : 5 }, "subtotal" : 0.0 }
{ "_id" : { "period" : "04/2019", "category" : 3 }, "subtotal" : 50 }
{ "_id" : { "period" : "03/2019", "category" : 4 }, "subtotal" : 0.0 }
{ "_id" : { "period" : "04/2019", "category" : 0 }, "subtotal" : 0.0 }
{ "_id" : { "period" : "03/2019", "category" : 0 }, "subtotal" : 0.0 }
{ "_id" : { "period" : "03/2019", "category" : 5 }, "subtotal" : 0.0 }
{ "_id" : { "period" : "02/2019", "category" : 1 }, "subtotal" : 129.44 }
{ "_id" : { "period" : "02/2019", "category" : 3 }, "subtotal" : 31.35 }
{ "_id" : { "period" : "03/2019", "category" : 2 }, "subtotal" : 0.0 }
{ "_id" : { "period" : "02/2019", "category" : 0 }, "subtotal" : 0.0 }
{ "_id" : { "period" : "01/2019", "category" : 3 }, "subtotal" : 50 }
{ "_id" : { "period" : "01/2019", "category" : 5 }, "subtotal" : 0.0 }
{ "_id" : { "period" : "12/2018", "category" : 1 }, "subtotal" : 132.44 }
{ "_id" : { "period" : "02/2019", "category" : 2 }, "subtotal" : 0.0 }
{ "_id" : { "period" : "04/2019", "category" : 1 }, "subtotal" : 72.58 }
{ "_id" : { "period" : "12/2018", "category" : 5 }, "subtotal" : 0.0 }
{ "_id" : { "period" : "01/2019", "category" : 0 }, "subtotal" : 0.0 }
{ "_id" : { "period" : "11/2018", "category" : 4 }, "subtotal" : 0.0 }
{ "_id" : { "period" : "02/2019", "category" : 4 }, "subtotal" : 0.0 }
{ "_id" : { "period" : "02/2019", "category" : 5 }, "subtotal" : 0.0 }
{ "_id" : { "period" : "01/2019", "category" : 1 }, "subtotal" : 161.81 }
{ "_id" : { "period" : "11/2018", "category" : 1 }, "subtotal" : 61.54 }
{ "_id" : { "period" : "11/2018", "category" : 5 }, "subtotal" : 0.0 }
{ "_id" : { "period" : "11/2018", "category" : 3 }, "subtotal" : 62 }
{ "_id" : { "period" : "12/2018", "category" : 4 }, "subtotal" : 0.0 }
{ "_id" : { "period" : "01/2019", "category" : 2 }, "subtotal" : 0.0 }
{ "_id" : { "period" : "11/2018", "category" : 0 }, "subtotal" : 0.0 }
{ "_id" : { "period" : "03/2019", "category" : 1 }, "subtotal" : 92.84 }
{ "_id" : { "period" : "05/2019", "category" : 7 }, "subtotal" : 0.0 }
{ "_id" : { "period" : "01/2019", "category" : 4 }, "subtotal" : 0.0 }
{ "_id" : { "period" : "11/2018", "category" : 2 }, "subtotal" : 0.0 }
{ "_id" : { "period" : "04/2019", "category" : 2 }, "subtotal" : 0.0 }
{ "_id" : { "period" : "12/2018", "category" : 2 }, "subtotal" : 0.0 }
{ "_id" : { "period" : "12/2018", "category" : 6 }, "subtotal" : 0.0 }
{ "_id" : { "period" : "12/2018", "category" : 0 }, "subtotal" : 0.0 }

更新2 (工作答案授予Mickl)

1 个答案:

答案 0 :(得分:1)

您需要$facet来运行两个单独的管道,然后“合并”结果。第一个是您与$ group的常规聚合,第二个应该计算所有必需的category/period值。然后,您可以将$map$filter一起运行以合并结果,而将$replaceRoot$ifNull进行合并以整形数据。

db.exps.aggregate([
    {
        $facet: {
            grouped: [
                { $match: { category: { $in: [1,3] } } },
                { $group: { _id: { period: "$period", category: "$category" }, subtotal: { $sum: "$amount" } } }
            ],
            periods: [
                { $group: { _id: null, period: { $addToSet: "$period" } } },
                { $addFields: { category: [ 1, 3 ] } },
                { $unwind: "$category" },
                { $unwind: "$period" }
            ]
        }
    },
    {
        $project: {
            data: {
                $map: {
                    input: "$periods",
                    as: "p",
                    in: {
                        $let: {
                            vars: { 
                                group: { 
                                    $filter: { 
                                        input: "$grouped", 
                                        as: "g", 
                                        cond: { 
                                            $and: [ 
                                                { $eq: [ "$$p.period", "$$g._id.period" ] } ,
                                                { $eq: [ "$$p.category", "$$g._id.category" ] }
                                            ] 
                                        } 
                                    } 
                                } 
                            },
                            in: {
                                period: "$$p.period",
                                category: "$$p.category",
                                group: { $arrayElemAt: [ "$$group", 0 ] }
                            }
                        }
                    }
                }
            }
        }
    },
    {
        $unwind: "$data"
    },
    {
        $replaceRoot: {
            newRoot: "$data"
        }
    },
    {
        $project: {
            period: 1,
            category: 1,
            amount: { $ifNull: [ "$group.subtotal", 0 ] }
        }
    },
    {
        $sort: {
            category: 1,
            period: 1 
        }
    }
])

输出:

{ "period" : "01/2019", "category" : 1, "amount" : 161.81 }
{ "period" : "02/2019", "category" : 1, "amount" : 129.44 }
{ "period" : "03/2019", "category" : 1, "amount" : 92.84 }
{ "period" : "04/2019", "category" : 1, "amount" : 72.58 }
{ "period" : "05/2019", "category" : 1, "amount" : 0 }
{ "period" : "12/2018", "category" : 1, "amount" : 0 }
{ "period" : "01/2019", "category" : 3, "amount" : 50 }
{ "period" : "02/2019", "category" : 3, "amount" : 31.35 }
{ "period" : "03/2019", "category" : 3, "amount" : 0 }
{ "period" : "04/2019", "category" : 3, "amount" : 50 }
{ "period" : "05/2019", "category" : 3, "amount" : 413 }
{ "period" : "12/2018", "category" : 3, "amount" : 0 }