What I want to do is query the whole of field3 but also includefield1 and field2 as sub-fields of field3, even though the data stored does not have field1 and field2 as sub-fields for field3. I’m trying to avoid to have to do this as post-processing where I copy/move the data into field3.field1 and field3.field2. So here’s how the final query should look like:
To give some context as to why I want to do this, it’s part of building a Graphql API, and the schema would require that field1 and field2 are sub-fields of field3. I’m trying to avoid having to do unnecessary post-processing to get them as sub-fields of field3. If I can accomplish this with a simple query call that would be great.
But I think this might need an aggregation approach instead?
Assuming the structure above matches your sample structure, to achieve the result you described you can first $match to filter the results, use $addFields to append to the field3 object and then use a $project to remove field1 and field2 from the top level of the resulting document: