猫鼬总订单以计算订购产品的利润

时间:2020-02-25 03:51:40

标签: mongodb mongoose aggregation-framework

我有两个收藏。 产品集合的示例如下:

[Fact]
public async Task ExecuteAsync_Test() {

    IServiceCollection services = new ServiceCollection();
    services.AddSingleton<IConfiguration>(_config);

    //...

下面是 Order 集合的示例:

{
  "_id": "5da3e8e90ddb8a7bb9b404d1",
  "updatedAt": "2020-01-07T09:20:02.170Z",
  "createdAt": "2019-10-14T03:18:01.421Z",
  "brief": {
    "category": ",Groceries,Chocolate",
    "images": [ "m & m chocolate peanut 45g.jpg" ],
    "stock": 10,
    "cost": 1.05,
    "price": 1.75,
    "name": "M&M's Chocolate Peanut",
    "code": "SHFP01-019"
  }
}

任务是找出特定买家的订单并计算所获总利润(利润的计算方法为: { "_id" : ObjectId("5e26558fc63ab131c518b588"), "updatedAt" : ISODate("2020-01-21T08:46:32.734Z"), "createdAt" : ISODate("2020-01-21T01:36:15.817Z"), "__v" : 0, "buyer_id" : ObjectId("5dd214a18c34403071166ebd"), "OrderNo" : "ORD-210", "products" : [ { "variants" : [ { "order_qty" : 1 } ], "product" : { "_id" : ObjectId("5d8197f30ddb8a7bb9b401d4"), "brief" : { "category" : ",Drinks,Beverages", "stock" : 10, "price" : 10.5, "name" : "Yeo's Sugar Cane ", "code" : "YEO113", "images" : [ "Yeo's Sugar Cane 24x250ml.jpg" ], } }, }, { "variants" : [ { "order_qty" : 1 } ], "product" : { "_id" : ObjectId("5d819a760ddb8a7bb9b401e0"), "brief" : { "category" : ",Drinks,Beverages", "stock" : 9, "price" : 10.5, "name" : "Yeo's Chrysanthemum Luo Han Guo Tea", "code" : "YEO118", "images" : [ "Yeo's Chrysanthemum Luo Han Guo 24x250ml.jpg" ], } }, }, { "variants" : [ { "order_qty" : 1 } ], "product" : { "_id" : ObjectId("5d81991d0ddb8a7bb9b401d8"), "brief" : { "category" : ",Drinks,Beverages", "stock" : 10, "price" : 10.5, "name" : "Yeo's Snow Pear", "code" : "YEO115", "images" : [ "Yeo's Snow Pear 24x250ml.jpg" ], } }, } ], "commission_status" : "Approved", "status" : "Paid" } 的总和-(product_cost* order_quantity)的总和。

我使用了此功能,但结果不正确。

(product_price * order_quantity)

0 个答案:

没有答案