mongo-cursor-pagination
mongo-cursor-pagination copied to clipboard
Populate with Mongoose
Hi!
How can I Populate my nested objects with Mongoose?
Thank you for your answer!
I'm unfamiliar with mongoose, but I think you should be able to call populate on the result within the pagination function. Can you post a snippet of code that you're not having luck with?
I managed to get it working by doing this. In this function I am fetching some quotes and populating it with the character details who said the quote:
const quotes = async (_, { limit, next }) => {
return Quote.paginate({ limit: limit, next: next })
.then((data) => {
data.results = data.results
.map(quote => Quote.populate(quote, [{ path: 'character' }]))
return data
})
}
Is this a good, optimized solution? If not, how can I manage to solve this in an optimized way?
Thank you for the answers in advance!