'pagination populate subdocument nodejs mongodb

#nodejs make pagination in populate subdocument in node js

//hi,thank for your attention // this is my code i want to make pagination for subdocument users

const schema = new mongoose.Schema({
  name: { type: String, required: true },
  users: [{ type: mongoose.Types.ObjectId, ref: 'User' }],// array of user
}, { timestamps: true })

module.exports = mongoose.model('Company', schema)

///////////////////////////////////////////////////

exports.users = async (req, res) => 
{
  await Company.findById(req.params.id).populate(['users'])
        .exec(async (err, company) => {
           if (err) {
                console.error(err)
                return res.status(500).json({ message: "Impossible .", error: err  } )
           }
 
        for (user of company.users) {
            await user.populate('agence').execPopulate()
        }
    return res.json(company.users)
  })
}

i want to pagination for array of users

exports.users = async (req, res) => 
{
  await Company.findById(req.params.id).populate([
  {
        path: 'user',
        select: 'user',
        model:'User',
        options: {
            sort:{ },
            skip: 5,
            limit : 10
        },
        match:{
           
        }
    }
  , 'users']) .exec(async (err, company) => {
           if (err) {
                console.error(err)
                return res.status(500).json({ message: "Impossible .", error: err  } )
           }
 
        for (user of company.users) {
            await user.populate('agence').execPopulate()
        }
    return res.json(company.users)
  })
}

but is not work, please help



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source