In my current aggregation setup, I am using the following simplified code:
Model.aggregate([
{
$lookup: {
from: 'orders',
localField: '_id',
foreignField: 'customer',
as: 'orders',
},
},
{
$project: {
openOrders: {
$filter: {
input: '$orders',
as: 'order',
cond: { $eq: ['$$order.status', 'open'] },
},
},
},
},
])
This code returns a structure that looks like this:
{
_id: ...,
openOrders: [
[Object], [Object]
],
}
The `[Object]` entries are simply the full objects retrieved from the database with all their properties intact.
My goal is to modify the output so that only the `_id` fields of these objects are returned:
{
_id: ...,
openOrders: [
_id: ...,
_id: ....
],
}
UPDATE: Ideally, I would like the final result to look like this:
{
_id: ...,
openOrders: [
{ _id: ... },
{ _id: ... }
],
}
I have attempted to add an additional `$project` stage at different points in the aggregation pipeline without success. If anyone has any suggestions on how to achieve this, your help would be greatly appreciated.