index.js 394 B

123456789101112131415161718
  1. const cloud = require('wx-server-sdk');
  2. cloud.init({
  3. env: cloud.DYNAMIC_CURRENT_ENV
  4. });
  5. const db = cloud.database();
  6. const $ = db.command.aggregate;
  7. // 聚合记录云函数入口函数
  8. exports.main = async (event, context) => {
  9. // 返回数据库聚合结果
  10. return db.collection('sales').aggregate()
  11. .group({
  12. _id: '$region',
  13. sum: $.sum('$sales')
  14. })
  15. .end();
  16. };