Reputation: 1433
I'm trying to retrieve only matched object from the mongoose query. I tried different approaches but still I'm unable to do so.
Response right now:
[
{
"_id": "61ac08008c3d6ad03a20c8f7",
"pages": [
{
"_id": "61ac1dcf45b78934dca130c4",
"page_number": "2",
"page_content": "Rasdsa"
}
]
}
]
Expected Response:
"pages": [
{
"_id": "61ac1dcf45b78934dca130c4",
"page_number": "2",
"page_content": "Rasdsa"
}
]
It will be more better if we get only unwind object from query.
Current query
await BooksModel.find( { book_id:"2"},{ pages: { $elemMatch: { page_number: "2" } }}, {"$project": {"pages":"1"}}).exec();
Upvotes: 0
Views: 370
Reputation: 8528
You can exclude the _id
by providing a 0 as the value in the project stage.
Like:
await BooksModel.find( { book_id:"2"},{ pages: { $elemMatch: { page_number: "2" } }}, {"$project": {"pages":"1", "_id": "0"}}).exec();
Upvotes: 1