92.8k views
5 votes
How do you exclude fields in the $project stage?

1 Answer

4 votes

Final answer:

To exclude fields in the $project stage, set their values to 0.

Step-by-step explanation:

In the $project stage of the MongoDB aggregation pipeline, you can exclude fields by setting their values to 0. Here is an example:

{ $project: { fieldName1: 0, fieldName2: 0 } }

This will exclude fieldName1 and fieldName2 from the output documents. You can specify multiple fields to exclude by listing them separated by commas.

User Dory Daniel
by
8.0k points