mongo-cursor-pagination icon indicating copy to clipboard operation
mongo-cursor-pagination copied to clipboard

Populate with Mongoose

Open Gyurmatag opened this issue 5 years ago • 2 comments

Hi!

How can I Populate my nested objects with Mongoose?

Thank you for your answer!

Gyurmatag avatar Dec 23 '20 12:12 Gyurmatag

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?

rfreebern avatar Dec 29 '20 19:12 rfreebern

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!

Gyurmatag avatar Jan 04 '21 18:01 Gyurmatag