我编写了一个脚本,它将交易展开到订单项,然后按产品汇总,以及产品销售额的总和数量和总收入。然后我通过partyuuid(公司)和itemTotalQuantity应用排序。
我的问题是,我如何才能使每个公司,我只获得十大畅销产品?我已经应用了这种类型,每个partyuuid我只需要10个产品。感谢
db.testCanonical.aggregate(
{ $match : {
objectClass: "Goods & Services Transaction"
,objectCategory: "Sales"
,objectType: "Transaction"
// , transactionDate.objectTransactionDate: {$gt: "2016-05-01T12:00:00.000Z", $lt: "2016-05-17T12:00:00.000Z"}
}
},
{ $project: {_id: 1,partyuuid: 1,objectOrigin: 1,goodsAndServices: 1}},
{ $unwind: "$goodsAndServices" }
,
{$group: {
_id: {
partyuuid: "$partyuuid"
, objectOrigin: "$objectOrigin"
, itemName : "$goodsAndServices.itemName"
}
, "itemTotalQuantity": {$sum: "$goodsAndServices.itemQuantity"}
,"itemGrossTotalValue": {$sum: "$goodsAndServices.itemTotalPrice"}
}},
{
$project: {
"_id":0,
partyuuid: "$_id.partyuuid",
itemName:"$_id.itemName",
objectOrigin: "$_id.objectOrigin",
itemTotalQuantity: "$itemTotalQuantity",
itemGrossTotalValue: "$itemGrossTotalValue"
}
},
{$sort:{partyuuid: 1, itemTotalQuantity: -1}}
//,{$limit: 5}
//,{$out: "entity_datapoints"}
)
如果您需要此脚本正在处理的文档副本,请与我们联系。再次感谢
根据要求,请参阅以下商品&上述代码适用的服务交易。
{
"_id" : ObjectId("5738fcff1a21db15b5c45b4b"),
"objectClass" : "Goods & Services Transaction",
"objectCategory" : "Sales",
"objectType" : "Transaction",
"objectOrigin" : "kounta",
"objectOriginCategory" : "Point of Sale",
"partyuuid" : "KountaPartyUUIDGoesHere",
"connectionuuid" : "5738fc661a21db15b5c45b49",
"objectTransactionDate" : ISODate("2016-05-16T04:03:56.201Z"),
"objectLastUpdated" : ISODate("2016-05-16T04:04:29.201Z"),
"objectCreationDate" : ISODate("2016-05-15T22:49:35.665Z"),
"statuses" : {
"objectTransactionStatus" : "COMPLETE"
},
"goodsAndServices" : [
{
"itemIdentifier" : 2143460.0000000000000000,
"itemName" : "Cappuccino",
"itemQuantity" : 1,
"itemUnitNettPrice" : 3.4800000000000000,
"itemTotalPrice" : 3.4800000000000000,
"itemTaxTotal" : 0.5200000000000000
},
{
"itemIdentifier" : 2143460.0000000000000000,
"itemName" : "Cappuccino",
"itemQuantity" : 1,
"itemUnitNettPrice" : 3.4800000000000000,
"itemTotalPrice" : 3.4800000000000000,
"itemTaxTotal" : 0.5200000000000000
},
{
"itemIdentifier" : 2143460.0000000000000000,
"itemName" : "Cappuccino",
"itemQuantity" : 1,
"itemUnitNettPrice" : 3.4800000000000000,
"itemTotalPrice" : 3.4800000000000000,
"itemTaxTotal" : 0.5200000000000000
},
{
"itemIdentifier" : 2143460.0000000000000000,
"itemName" : "Cappuccino",
"itemQuantity" : 1,
"itemUnitNettPrice" : 3.4800000000000000,
"itemTotalPrice" : 3.4800000000000000,
"itemTaxTotal" : 0.5200000000000000
},
{
"itemIdentifier" : 2143470.0000000000000000,
"itemName" : "Espresso",
"itemQuantity" : 1,
"itemUnitNettPrice" : 2.6099999999999999,
"itemTotalPrice" : 2.6099999999999999,
"itemTaxTotal" : 0.3900000000000000
},
{
"itemIdentifier" : 2143470.0000000000000000,
"itemName" : "Espresso",
"itemQuantity" : 1,
"itemUnitNettPrice" : 2.6099999999999999,
"itemTotalPrice" : 2.6099999999999999,
"itemTaxTotal" : 0.3900000000000000
},
{
"itemIdentifier" : 2143470.0000000000000000,
"itemName" : "Espresso",
"itemQuantity" : 1,
"itemUnitNettPrice" : 2.6099999999999999,
"itemTotalPrice" : 2.6099999999999999,
"itemTaxTotal" : 0.3900000000000000
},
{
"itemIdentifier" : 2143470.0000000000000000,
"itemName" : "Espresso",
"itemQuantity" : 1,
"itemUnitNettPrice" : 2.6099999999999999,
"itemTotalPrice" : 2.6099999999999999,
"itemTaxTotal" : 0.3900000000000000
},
{
"itemIdentifier" : 2143470.0000000000000000,
"itemName" : "Flat White",
"itemQuantity" : 1,
"itemUnitNettPrice" : 3.4800000000000000,
"itemTotalPrice" : 3.4800000000000000,
"itemTaxTotal" : 0.5200000000000000
},
{
"itemIdentifier" : 2143470.0000000000000000,
"itemName" : "Flat White",
"itemQuantity" : 1,
"itemUnitNettPrice" : 3.4800000000000000,
"itemTotalPrice" : 3.4800000000000000,
"itemTaxTotal" : 0.5200000000000000
},
{
"itemIdentifier" : 2143470.0000000000000000,
"itemName" : "Flat White",
"itemQuantity" : 1,
"itemUnitNettPrice" : 3.4800000000000000,
"itemTotalPrice" : 3.4800000000000000,
"itemTaxTotal" : 0.5200000000000000
},
{
"itemIdentifier" : 2143470.0000000000000000,
"itemName" : "Macchiato",
"itemQuantity" : 1,
"itemUnitNettPrice" : 2.6099999999999999,
"itemTotalPrice" : 2.6099999999999999,
"itemTaxTotal" : 0.3900000000000000
},
{
"itemIdentifier" : 2143470.0000000000000000,
"itemName" : "Macchiato",
"itemQuantity" : 1,
"itemUnitNettPrice" : 2.6099999999999999,
"itemTotalPrice" : 2.6099999999999999,
"itemTaxTotal" : 0.3900000000000000
},
{
"itemIdentifier" : 2143470.0000000000000000,
"itemName" : "Macchiato",
"itemQuantity" : 1,
"itemUnitNettPrice" : 2.6099999999999999,
"itemTotalPrice" : 2.6099999999999999,
"itemTaxTotal" : 0.3900000000000000
},
{
"itemIdentifier" : 2143470.0000000000000000,
"itemName" : "Long Black",
"itemQuantity" : 1,
"itemUnitNettPrice" : 3.4800000000000000,
"itemTotalPrice" : 3.4800000000000000,
"itemTaxTotal" : 0.5200000000000000
},
{
"itemIdentifier" : 2143470.0000000000000000,
"itemName" : "Macchiato",
"itemQuantity" : 1,
"itemUnitNettPrice" : 2.6099999999999999,
"itemTotalPrice" : 2.6099999999999999,
"itemTaxTotal" : 0.3900000000000000
},
{
"itemIdentifier" : 2143470.0000000000000000,
"itemName" : "Macchiato",
"itemQuantity" : 1,
"itemUnitNettPrice" : 2.6099999999999999,
"itemTotalPrice" : 2.6099999999999999,
"itemTaxTotal" : 0.3900000000000000
},
{
"itemIdentifier" : 2143470.0000000000000000,
"itemName" : "Macchiato",
"itemQuantity" : 1,
"itemUnitNettPrice" : 2.6099999999999999,
"itemTotalPrice" : 2.6099999999999999,
"itemTaxTotal" : 0.3900000000000000
},
{
"itemIdentifier" : 2143470.0000000000000000,
"itemName" : "Mocha",
"itemQuantity" : 1,
"itemUnitNettPrice" : 3.4800000000000000,
"itemTotalPrice" : 3.4800000000000000,
"itemTaxTotal" : 0.5200000000000000
},
{
"itemIdentifier" : 2143470.0000000000000000,
"itemName" : "Mocha",
"itemQuantity" : 1,
"itemUnitNettPrice" : 3.4800000000000000,
"itemTotalPrice" : 3.4800000000000000,
"itemTaxTotal" : 0.5200000000000000
}
],
"transactionGrossTotal" : 70,
"transactionTotalPayments" : 70,
"payments" : [
{
"paymentMethod" : "Cash",
"paymentAmount" : 70,
"paymentExtras" : 0,
"paymentDate" : "2016-05-15T22:49:26+00:00"
}
],
"party" : {
"customer" : {},
"staff" : {
"staffid" : 53810
}
},
"resources" : {
"device" : {
"deviceId" : 1031390.0000000000000000
},
"location" : {
"locationId" : 25052
}
}
}