聚合联表查询,返回数据超过100条,程序代码这么写有什么问题?怎么返回不了数据
发布于 5 年前 作者 shenjuan 4430 次浏览 来自 官方Issues

// 云函数入口文件

const cloud = require(‘wx-server-sdk’)

cloud.init()

const db = cloud.database({env:‘pmis - 4c63d’})

const $ = db.command.aggregate

const _=db.command

const MAX_LIMIT = 100

// 云函数入口函数

exports.main = async(event, context) => {

 

  const countResult = await db.collection(‘jxgs’).aggregate()

    .lookup({

      from: ‘jxgsquexian’,

      let: {

        bdzname: ‘$bdzname’,

        sbyxbh: ‘$sbyxbh’

      },

      pipeline: $.pipeline()

        .match(_.expr($.and([

          $.eq([’$bdzname’, ‘$$bdzname’]),

          $.eq([’$sbyxbh’, ‘$$sbyxbh’])

        ])))

        .done(),

      as: ‘quanxianList’,

    }).count(“total”).end();

  const total = countResult.total

  // 计算需分几次取

  const batchTimes = Math.ceil(total / MAX_LIMIT)

  // 承载所有读操作的 promise 的数组

  const tasks = []

  for (let i = 0; i < batchTimes; i++) {

    const promise = db.collection(‘jxgs’).aggregate()

      .lookup({

        from: ‘jxgsquexian’,

        let: {

          bdzname: ‘$bdzname’,

          sbyxbh: ‘$sbyxbh’

        },

        pipeline: $.pipeline()

          .match(_.expr($.and([

            $.eq([’$bdzname’, ‘$$bdzname’]),

            $.eq([’$sbyxbh’, ‘$$sbyxbh’])

          ])))

          .done(),

        as: ‘quanxianList’,

      }).skip(i * MAX_LIMIT).limit(MAX_LIMIT).end();

    tasks.push(promise);

  }

  // 等待所有

  return (await Promise.all(tasks)).reduce((acc, cur) => {

    return {

      data: acc.data.concat(cur.data),

      errMsg: acc.errMsg,

    }

  })

 

}

1 回复

干嘛要分几次查询

回到顶部