Better alternatives for a projection stage
The typical method for specifying fields to include or exclude in the MongoDB aggregation framework has been the $project
stage. This was the only way to define which fields to keep or omit for many earlier versions of MongoDB. However, $project
comes with a few usability challenges:
$project
can be confusing and non-intuitive. You can only choose to include fields or exclude fields in a single stage, but not both. However, there is one exception, where you can exclude the_id
field and still define other fields to include. This only applies to the_id
field and this ambiguity makes$project
unintuitive to apply.$project
can be verbose and inflexible. If you want to define one new field or revise one field, you will have to name all other fields in the projection to include. If each input record has 100 fields and the pipeline needs to employ a$project
stage for the first time, defining these files can become time consuming....