I am trying to figure out how to use the $addFields
operation within a function I'm using to update a document. The logic to generate this field is simple enough, I just pull out the last object in an array, like so:
{
$addFields: { activeWorkflow: { $arrayElemAt: ["$workflow", -1.0] } }
}
Now I need to figure out how to get this aggregation into my update function, the relevant code of which looks like this:
updatedDoc = await request.findOneAndUpdate(
StaffMember,
{
_id: request.parameters.id
},
req,
{
new: true
}
);
What I tried doing was this:
updatedDoc = await request.findOneAndUpdate(
StaffMember,
{
_id: request.parameters.id
},
req,
{
new: true
}
);
await request.aggregate(updatedDoc, [
{
$addFields: { activeWorkflow: { $arrayElemAt: ["$workflow", -1.0] } }
}
]);
... but this gives me an error:
TypeError: mongooseModelObject.aggregate is not a function
Would I use the $addFields operator with $set, like so?
updatedDoc = await request.findOneAndUpdate(
StaffMember,
{
_id: request.parameters.id
},
{
$set: {
req,
$addFields: { activeWorkflow: { $arrayElemAt: ["$workflow", -1.0] } }
}
},
{
new: true
}
);
How does one run an aggregation as part of a function like this? A simple example would help me understand how this needs to be done -- if indeed it can be done.
Of course, there are other ways to do this, but I'd like to take advantage of a mongo-specific way to handle this, using a mongo operation like $addFields
. What would this look like?
By the way, the wrapper function I'm using with aggregate looks like this:
async aggregate(mongooseModelObject, aggregateObject = {}) {
try {
return await mongooseModelObject.aggregate(aggregateObject).exec();
} catch (err) {
this.sendError(err);
}
}