I have a collection called “Binders”
In this collection I have a field called “docs” that references a “Docs” schema
I want to pull a “Binder” by id with findOne() which is no problem.
But I want to limit the returned “Docs” by a field comparison
I am somewhat new to mongoose but if i had pseudo my query…
Binder.findOne({_id: req.binder._id}, {$where {docs.type = 'type1'}}, function(error, binder) {
// So this would return my full binder but only the docs where their .type = 'type1'
});
Where I get lost is the limiting based on a Schema
{$where {docs.type = 'type1'}}
So if I pulled a binder and it had 50 docs, but none of the docs were of type1 I would still get my binder info, however there would be no documents in the Docs field.
Hope that makes sense.
If I’m following you right and
docsis an array, then you can use the$elemMatchprojection operator to do this: