Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

populate all array of objects

I have a schema of concerts which I am populating with reviews using virtual populate which returns an array of reviews. Now on every review I again have to populate customerId to its name and avatar.

Here is my code

const populateReviews = await Concert.findById(id).populate({
    path: "reviews",
    options: {
      sort: { rating: -1, updatedAt: -1 },
    },
  });
const populateCustomers = await Customer.populate(populateReviews, {
  path: "reviews.customerId",
  options: {
    select: { name: 1, avatar: 1 },
  },
});

And here is the output of above code.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

I have to populate customerId. Please help me with this.

>Solution :

Populating across multiple levels would help you

Concert
 .findById(id)
 .populate({
    path: "reviews",
    options: {
      sort: { rating: -1, updatedAt: -1 },
      },
    populate : {
    path : 'customerId',
    options: {
       select: { name: 1, avatar: 1 },
      },
     }
  })
 .exec(function (err, res) {

 })
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading