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

Node.js MongoDB聚合:如何在MongoDB聚合中应用分页,node.js,mongodb,mongoose,aggregation-framework,Node.js,Mongodb,Mongoose,Aggregation Framework,这是我的查询,一切正常,但不知道如何在使用聚合时应用分页: try { let data =[ { $match : { $or : [{'firstName' : {$regex : '.*' + req.body.searchKey + '.*', '$options' : 'i'}}, {'lastName' : {$regex : '.*' + req.body

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

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});

    }

我想对此查询应用分页。我第一次使用聚合。请帮忙,提前谢谢。

你可以这样做

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 });
    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 });

你可以这样做

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 });
    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 });

您想在
用户
列表或
处方
列表上分页?如果您可以共享用户(患者)的数据模型,user list@SarfraazIt将非常有用和处方。您想在
用户
列表或
处方
列表上分页?如果您可以共享用户(患者)和处方的数据模型,user list@SarfraazIt将非常有用。在这里,分页将仅应用于用户,或者更确切地说是患者。我想OP希望分页最终结果。我能预见的问题是,如果用户患者数据很大,它将打破查询并超出限制,因此分页将被打破。在没有获得记录总数的情况下,我们如何找到pagesCheck的总数,我已更新了答案,两种获得分页的方法这里,分页将只应用于用户,或者更确切地说是患者,我想OP希望对最终结果进行分页。我可以预见的问题是,如果用户患者数据很大,它将打破查询并超出限制,因此分页将被打破。在不获取记录总数的情况下,我们如何找到pagesCheck的总数,我已更新了答案,这是两种获取分页的方法