我在API中使用聚合mongoose mongoDb后出错

utugiqy6  于 2023-11-17  发布在  Go
关注(0)|答案(1)|浏览(121)

“错误”:{“ok”:0,“code”:5107201,“codeName”:“Location5107201”,“$clusterTime”:{“clusterTime”:{“$timestamp”:“7292060947630260235”},“signature”:{“hash”:“HQW2T+0z31Kf8q6iLViqdu0uYtQ=",“keyId”:{“low”:2,“high”:1683641553,“unsigned”:© 2019 www.szb.com版权所有并保留所有权利

let page = 1;
let limit = 10;
let sort = {};
let skip =(page-1)*limit
//req.query.sort is coming as 'name' or '-name'
  if (req.query.sort) {
     const key = req.query.sort;
     if (req.query.sort[0] === "-") {
        sort[`${key.substring(1)}`] = -1;
        sort["_id"] = 1;
     } else {
        sort[`${key}`] = 1;
        sort["_id"] = 1;
     }
  } else {
     sort.dateCreated = 1;
  }

const reviewsList = await Review.aggregate([
     { $match: filter },
     {
        $lookup: {
           from: "users",
           localField: "user",
           foreignField: "_id",
           as: "user",
        },
     },
     {
        $unwind: "$user",
     },
     {
        $lookup: {
           from: "products",
           localField: "product",
           foreignField: "_id",
           as: "product",
        },
     },
     {
        $unwind: "$product",
     },
     {
        $sort: sort,
     },
     { $skip: skip },
     { $limit: limit },
  ]);

字符串

yhived7q

yhived7q1#

问题是page和limit都是String

const reviewsList = await Review.aggregate([
          .......
         { $skip: +skip },
         { $limit: +limit },
      ]);

字符串

相关问题