I am working on an aggregation pipeline to compile data to push to the frontend for a chart to render, which involves 3 collections (one of which is just a reference to the other 2). Individually on the collections, I am able to get the expected response on the aggregation, but when trying to incorporate the aggregations together with $lookup, I keep getting the error:
query failed: (Location40323) A pipeline stage specification object must contain exactly one field.
From what I’m reading in other user’s threads on this error, its either a missing bracket, or a property being set outside the appropriate bracket. However, for the life of me, I cannot find any missing brackets. I’m still learning mongoDB, and (coding in general…) so I’m certain its some silly mistake/oversight, but I’ve been running through the code over and over and coming up short. If anyone can help point out the errors I’m making, I’d greatly appreciate it.
Yup, was the brackets, I was tripping myself up adding the {} in the $lookup pipelines. Each time I went to the add the brackets, I was accidentally wrapping each condition within a greater object of the pipeline array, for example :
pipeline: [
{
{$match:… },
{$lookup: …}
Just needed to take a break and come back to it, was staring too long and got lost in the braces…
Well the initial collection has a lot of other properties and at scale the documents would exceed the 16mb limit if I nested the other 2 collections, so I tie them together with ref’s. As far as I’m aware, that is the preferred mongo way of approaching it, but I am always open to suggestions.