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

205
Views
How to remove a field from field that is being populated mongoose

so

async findAll(req, res) {
        await WorkModel.find({})
            .sort('when')
            .populate({path: 'artists', populate: { path: 'categories'}})
            .then(response => {
                return res.status(200).json(response);
            })
            .catch(error => {
                return res.status(500).json(error);
            });
    }

I need in this function remove works from the population that is happen is artists, but when I try like passing

{path: 'artists -works', populate: { path: 'categories'}}

I'm getting {} response, or even when I try

{path: 'artists -works', populate: { path: 'categories'}}, '-works'

in the populate

the method just get a stackTrace, I'm out of clues rn, please help me out.

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

0

If you want to select only works field from categories collection, using populate. you can do it using select !

Try this code it's help you ! For more help mongoosejs.com

async findAll(req, res) {
        await WorkModel.find({})
            .sort('when')
            .populate({path: 'artists', populate: { path: 'categories', select: 'works'}})
            .then(response => {
                return res.status(200).json(response);
            })
            .catch(error => {
                return res.status(500).json(error);
            });
    }
about 4 years ago · Juan Pablo Isaza Report

0

So the solution to remove the works from many-to-many relation in artists was

 async findAll(req, res) {
        await WorkModel.find({})
            .sort('when')
            .populate({path: 'artists', select:'-works', populate: { path: 'categories'}})
            .then(response => {
                return res.status(200).json(response);
            })
            .catch(error => {
                return res.status(500).json(error);
            });
    }

Thanks for all the comments, really helped out.

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!