I have the following aggregation stage that I’d like to add to my pipeline, but it keeps failing. When I use the MongoDB Compass Aggregation GUI, everything works as it should. I even exported that pipeline from the GUI and am using It the same way In my project but I keep getting this error:
MongoServerError: A pipeline stage specification object must contain exactly one field.
I even tried to hard-coded a productId in the $match
value (the same way I did in the GUI), but still nothing.
What am I doing wrong here?
Aggregation stage:
const formatIncludedInBomStage = ({ includedInBom }) => {
const includedInBomStage = {
$unwind: {
path: '$finale.bomItems',
},
$match: {
'finale.bomItems.productId': includedInBom,
},
}
return includedInBomStage
}
2
Answers
Solution:
Pipeline stages are an array, you are using multiple object properties instead.
unwind
should be one stage object in the array, andmatch
another object in the array.