Hey, I want to show all the document fields in the output but some of the fields are not present in some of the document then how can i display those fields with “NULL”. Here is the example: Sample document:
Simply do exactly the same king of $project with $ifNull but for each value you want null.
It will get complex and ugly very fast as you will need to use $range to create you array indexes, $map to map each element to the existing element or to your default null using $ifNull.
But, my personal opinion, is that this king of null-ishing cosmetic manipulations are better done on the application data access layer rather than the data server. It is easier to scale the state-less data access layer rather than the server. And most of the time there is nothing to do on the application side, you just access the data and you get null or undefined in most languages. You absolutely gain nothing by having the data server do more work in order to send more useless data over the wire. This is reminiscence of SQL where all columns are there even when there is no data.