MongoDB聚合:如何在MongoDB聚合中应用分页

问题描述 投票:0回答:1

这是我的查询,一切正常,但不知道在使用聚合时如何应用分页:

try {
        let data =[
            {
            $match : {
                $or : [{'firstName' : {$regex : '.*' + req.body.searchKey + '.*', '$options' : 'i'}},
                {'lastName' : {$regex : '.*' + req.body.searchKey + '.*', '$options' : 'i'}}]
            }
          },{
            $lookup : {
             from : 'prescriptions',
             let: { patientId: '$_id' },

             pipeline: [ 
               {
                 $match: {  
                   $expr: {  $eq: ['$patientId','$$patientId']}
                 }       
                },  {           
                    $lookup : {
                     from : 'users',
                     let: { doctorId: '$doctorId' },
                     pipeline: [
                       {
                         $match: {  
                           $expr: {  $eq: ['$_id','$$doctorId']}
                         }       
                        },     
                     ],
                     as : 'doctorData'              
                    }
                }
             ],
             as : 'patientData'    
            },
           }

          ]

          let data1 = await userModel.aggregate(data);
          res.status(200).json({success : true, data : data1});

    }

我想对此查询应用分页。我第一次使用聚合。请帮助,在此先感谢。

node.js mongodb mongoose aggregation-framework
1个回答
0
投票

您可以这样操作

let page_no = 0;
let limit = 10;
let offset = page_no > 1 ? (page_no-1)*limit : 0;

let data = [
    {
        $match: {
            $or: [
                { firstName: { $regex: '.*' + req.body.searchKey + '.*', $options: 'i' } },
                { lastName: { $regex: '.*' + req.body.searchKey + '.*', $options: 'i' } }
            ]
        }
    },{
        $skip: offset
    },{
        $limit: limit
    },
    {
        $lookup: {
            from: 'prescriptions',
            let: { patientId: '$_id' },
            pipeline: [
                {
                    $match: {
                        $expr: { $eq: ['$patientId', '$$patientId'] }
                    }
                },
                {
                    $lookup: {
                        from: 'users',
                        let: { doctorId: '$doctorId' },
                        pipeline: [
                            {
                                $match: {
                                    $expr: { $eq: ['$_id', '$$doctorId'] }
                                }
                            }
                        ],
                        as: 'doctorData'
                    }
                }
            ],
            as: 'patientData'
        }
    }
];

let data1 = await userModel.aggregate(data);

let total = await userModel.find({
        $or: [
            { firstName: { $regex: '.*' + req.body.searchKey + '.*', $options: 'i' } },
            { lastName: { $regex: '.*' + req.body.searchKey + '.*', $options: 'i' } }
        ]
    }).countDocuments();

let is_next = offset+limit < total;
res.status(200).json({ success: true, data: data1, is_next:is_next });

这里,对于特定页面的数据,我们将page_no从0到N,

先跳过page_no *限制记录,然后再限制记录以聚合返回

我们获取要查询的记录的total_no,并将其与offset + limit匹配,以查看是否存在下一页

这是我所采用的一种方法,

对于其他方法,我们可以返回总页数,并返回相同的响应,例如这样

    let total = await userModel.find({
        $or: [
            { firstName: { $regex: '.*' + req.body.searchKey + '.*', $options: 'i' } },
            { lastName: { $regex: '.*' + req.body.searchKey + '.*', $options: 'i' } }
        ]
    }).countDocuments();

    let pages = Math.ceil(total/limit);
res.status(200).json({ success: true, data: data1, no_of_pages:pages });
© www.soinside.com 2019 - 2024. All rights reserved.