Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

234
Views
¿Hay alguna manera de paginar a través de documentos completos usando mongoose-paginate-v2?

Tengo modelos de usuario y publicación. User.favPosts es una matriz de referencias al modelo Post. Estoy llamando a paginate así:

 options = { populate: {path: 'favPosts'} }; const result = await User.paginate({}, options)

Y el resultado es un documento de usuario con publicaciones pobladas:

 { "docs": [ { "_id": "6299ffa5c2ca4cdeebd1f513", "name": "user", "email": "email@mail.com", "favPosts": [ { "_id": "629b299897f46f31761ad7a7", "title": "Available Test 5", "description": "Lorem ipsum dolor sit" }, { "_id": "629b1edf108e765744d2560d", "title": "Available Test 4", "description": "Lorem ipsum dolor sit" }, { "_id": "629b1c0027bf0eb197c057dd", "title": "Available Test 4", "description": "Lorem ipsum dolor sit" } ] } ], "totalDocs": 1, "offset": 0, "limit": 10, "totalPages": 1, "page": 1, "pagingCounter": 1, "hasPrevPage": false, "hasNextPage": false, "prevPage": null, "nextPage": null }

Entonces obtengo la paginación de los usuarios. Pero quiero obtener la paginación de los documentos completos (publicaciones), para poder obtener la cantidad de estos, el conteo de páginas, etc. Así que quiero que el resultado se vea así:

 { "docs": [ { "_id": "629b299897f46f31761ad7a7", "title": "Available Test 5", "description": "Lorem ipsum dolor sit" }, { "_id": "629b1edf108e765744d2560d", "title": "Available Test 4", "description": "Lorem ipsum dolor sit" }, { "_id": "629b1c0027bf0eb197c057dd", "title": "Available Test 4", "description": "Lorem ipsum dolor sit" } ], "totalDocs": 3, "offset": 0, "limit": 10, "totalPages": 1, "page": 1, "pagingCounter": 1, "hasPrevPage": false, "hasNextPage": false, "prevPage": null, "nextPage": null }

¿Alguien puede decirme si es posible hacer tal cosa a través de mongoose-paginate-v2?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Así es como lo resolví. Usé el módulo mongoose-aggregate-paginate-v2 en lugar de mongoose-paginate-v2:

 const aggregate = User.aggregate([ // without "await", so it will be a Promise { $match: { userId } }, // filter for users collection { // populating posts: $lookup: { from: 'posts', localField: 'favPosts', foreignField: '_id', as: 'documents', pipeline, // here's filter for posts }, }, // unwind + replaceRoot to get an array of posts to the root, so this would be the posts array instead of the users array { $unwind: '$documents', }, { $replaceRoot: { newRoot: '$documents' }, }, ]);

Y luego alimentando esta Promesa al paginador:

 const options = { page: 2, limit: 10 }; const result = await User.aggregatePaginate(aggregate, options);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!