收藏
回答

云函数 group 和 skip 组合使用?

group 阶段按当前月、日分组

假设当前日有1000条数据,那最终结果group 的 list 中会同时取出这1000条数据吗?

skip 是按所有分组中list.length求和的结果跳过吗?

app.router("GetBookedByAdmin", async (ctx, next) => {
      try {
         const month = new Date().getMonth() + 1;
         const { identity_id, skip, limit } = event.value;
         const { list } = await db
            .collection("TestingOrders")
            .aggregate()
            .match({
               "appointment.month": month,
               "nurse._id": identity_id,
               serviceState: status.TRIAGED,
            })
            .sort({ "appointment.day": -1 })
            .lookup({
               from: "Users",
               let: { user_id: "$user_id" },
               pipeline: $.pipeline()
                  .match(_.expr($.eq(["$_id", "$$user_id"])))
                  .limit(1)
                  .project({
                     _id: 0,
                     avatarUrl: 1,
                     nickName: 1,
                  })
                  .done(),
               as: "userInfo",
            })
            .replaceRoot({ newRoot: $.mergeObjects([$.arrayElemAt(["$userInfo", 0]), "$$ROOT"]) })
            .project({ userInfo: 0 })
            .group({
               _id: {
                  month: "$appointment.month",
                  day: "$appointment.day",
               },
               list: $.push({
                  _id: "$_id",
                  appointment: "$appointment",
                  doctor: "$doctor",
                  location: "$location",
                  serviceState: "$serviceState",
                  status: "$status",
                  symptoms: "$symptoms",
                  user_id: "$user_id",
                  avatarUrl: "$avatarUrl",
                  nickName: "$nickName",
               }),
            })
            .skip(skip)
            .limit(limit)
            .end();

         ctx.body = { data: list };
      } catch (error) {
         console.error("GetBookedByAdmin -> ", error);
         ctx.body = { data: error };
      }
   });

回答关注问题邀请回答
收藏

1 个回答

  • showms
    showms
    03-04

    按你的写法最终结果应该是list是按month/day分组得到的数组,skip是对分组结果进行分页,这个你再开发工具上测试一下就知道了

    03-04
    有用
    回复
登录 后发表内容