【问题标题】:Pagination from Nodejs Mongoose subdocument来自 Nodejs Mongoose 子文档的分页
【发布时间】:2022-02-01 15:10:20
【问题描述】:

使用 mongoose-paginate-v2 我正在尝试对集合中名为“Items”的子注释进行分页,但我没有成功。这是我的模型:

    const mongoosePaginate = require('mongoose-paginate-v2');

    const PettyCashItemsSchema = Schema (
    {
        pettyCashId:{
            type: Schema.Types.ObjectId,
            ref:'PettyCash',
            required: [true, 'La Caja Chica es Obligatoria']
        },
        user:{
            type: Schema.Types.ObjectId,
            ref:'Users',
            required: [true, 'El Usuario es obligatorio']
        },
        item: {
            type: Number,
            unique: true
        },    
        items:[{
            concept: {
                type: String,
                maxlength:50,
                required: [true, 'El Concepto es obligatorio']
            },
            incomeAmount:{
                type: Number,
                maxlength:50,
                default:0,
                required: [true, 'El Ingreso es obligatorio']
            },
            expenseAmount:{
                type: Number,
                maxlength:50,
                default:0,
                required: [true, 'El Egreso es obligatorio']
            },
            description: {
                type: String,
                maxlength:50,
                required: [true, 'La Observación es obligatoria']
            },
            status: {
                type: Boolean,
                default: true,
                required: [true, 'El Estatus es obligatorio']
            }
        }],
        status: {
            type: Boolean,
            default: true,
            required: [true, 'El Estatus es obligatorio']
        }
    }  
    );

我正在尝试以这种方式执行此操作,但它会返回整个文档,而不仅仅是我需要分页的子文档:

        const options = {
            lean: true,
            limit: 10,
            page: 1,
            read: {
              pref: 'secondary',
              items: [
                {
                    status: true,
                },
              ],
            },
          };
        
        let items = await PettyCashItems.paginate( { 'pettyCashId': pettyCashId }, options );
        
        res.json( items );  

直到现在我还不明白如何实现它,但我希望你能帮助我。谢谢。

【问题讨论】:

    标签: javascript node.js mongodb express mongoose


    【解决方案1】:

    我不确定 MongoDB 是否提供开箱即用的方法,但您可以使用聚合 $function 来实现此目的。

    PettyCashItems.aggregate([
        {
            $match: {
                pettyCashId: pettyCashId 
            }
        },
        {
            $addFields: {
                items: {
                    $function: {
                        body: function(items) {
                            var skip = (page - 1) * limit
                            var to = skip + limit
                            return {
                                docs: items.slice(skip, to), 
                                totalDocs: items.length
                            }
                        },
                        args: ['$items'],
                        lang: 'js'
                    }
                }
            }
        }
    ])
    

    如果你也想对PettyCashItems 进行分页,你可以在$addFields 阶段之前添加$skip$limit 阶段

    【讨论】:

      猜你喜欢
      • 2013-05-21
      • 1970-01-01
      • 2013-03-30
      • 1970-01-01
      • 1970-01-01
      • 2020-07-09
      • 2016-04-13
      • 2021-02-16
      • 2015-03-27
      相关资源
      最近更新 更多