How can I get only a specific array inside array of subdocuments?

Hey guys, I have a Project schema with an array of subdocuments (entries).

entries: [EntrySchema]

This EntrySchema has some data such as: username, email…

What Im trying to do is, with a given username, i need to pull out the EntrySchema of that user inside the array. (I mean, retrieve only the EntrySchema information of that user).

I tried the following query:

const exists = await Project.findOne({_id:id,owner,"entries.username":username}, {"entries.$": 1}).exec();

But the problem is, this query returns the whole project schema, with all entries for example, instead of only the information of the entry of the username Im querying…

How can I do that?

As already mentioned in your other threads:

And the link to $filter was already provided in one of your other threads:

You need to aggregate starting with a $match stage that has the same query as your findOne().

You then need a $project stage that $filter the entries array using the username in the condition.