Why mongo doesn't allow to use a $facet stage inside another $facet? Why mongo doesn't allow to use a $facet stage inside another $facet? mongodb mongodb

Why mongo doesn't allow to use a $facet stage inside another $facet?


It's not only about facets. In general it's not allowed to create subfields in an aggregation stage. For example the following attempt of creating a complex value fails.

$addField: {  a.b: "foo"}

And that makes quite sense, since a value can be a primitive type, a complex object or an array. Don't know if that is a satisfying answer to you, maybe it's a little consolation that this not only counts for facets, but for all assignments in general.

I want also say that your intention is plausible. Especially in a database system, where no schema is required and each document can differ from others, it would be nice to have that feature. If you actually manipulate data you can do so with set. But on your intermediate results, you are not allowed to perform that action. One can perceive that as a bit inconsistent.