This is my test collection
[
{
"_id": "637cbf94b4741277c3b53c6c",
"text": "outter",
"username": "test1",
"address": [
{
"text": "inner",
"username": "test2",
"_id": "637cbf94b4741277c3b53c6e"
}
],
"__v": 0
}
]
If I do
t1 = await doc.find({}, 'text').exec();
console.log(JSON.stringify(t1, null, 2));
I get
[
{
"_id": "637cbf94b4741277c3b53c6c",
"text": "outter"
}
]
So here it finds the parent text
.
Question
How do I get Mongoose to query the sub document instead of the parent?
2
Answers
You can project the sub document(s) like this:
See how it works on the playground example
If you only want the sub document, not even the _id of the parent: