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.
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);
});
}
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.