Is it possible to use group aggregation in invoke lamda function?

前端 未结 1 1075
不知归路
不知归路 2021-01-17 03:25

I have created three lambda functions 1).postData 2).like 3).comment. I am using invoke lambda function to combine three output. Find the below code for your reference.

相关标签:
1条回答
  • 2021-01-17 03:58

    You have two options here.

    1. Create one lambda that will run all three queries, and aggregate there (example of mongo grouping below).

    2. Parse your JSON response objects, if they have not been parsed already, and manually aggregate.

    Obviously #1 will be more performant, and is way more elegant.

    Mongo aggregate/group function example:

    db.sales.aggregate(
       [
          {
            $group : {
               _id : { month: { $month: "$date" }, day: { $dayOfMonth: "$date" }, year: { $year: "$date" } },
               totalPrice: { $sum: { $multiply: [ "$price", "$quantity" ] } },
               averageQuantity: { $avg: "$quantity" },
               count: { $sum: 1 }
            }
          }
       ]
    )
    

    If you decide to go the mongo grouping route in your lambda, please keep in mind that it uses in memory storage while it is calculating. If your data set is huge this will give you an error. If you run into that issue set allowDiskUse to true as the docs suggest.

    See:

    • https://docs.mongodb.com/manual/reference/operator/aggregation/group/
    0 讨论(0)
提交回复
热议问题