Relative Content

Tag Archive for node.jsmongodblookupaggregationgroup

Populate a ObjectId deep nested inside arrays and objects preserving document shape

I’m using MongoDB v5 with NodeJS (no Mongoose).
Let’s say I have a collection structured the following way. This specific setup (an ObjectId nested inside an object contained inside an array contained inside another object) is trickier to handle in an aggregation because nested.users can not be unwinded then rebuilt again into an array (because $group doesn’t support deep properties) and using $lookup with let/as/pipeline works only if as contains something different than nested.users.user, in which case the object loses it’s original shape.