Can you update the values of a mongodb document using $project?

In Chapter 1: Basic Aggregation - $match and $project

Shaping documents with $project

One of the incorrect answers about project was:

$project cannot be used to assign new values to existing fields.

But can we update the data to existing field values using project?

I felt like it can only be used for modifications and calculations in output view without changing the original mongo DB document.

Please illuminate me!

Hi @Vivek_Poddar,

Read the option like this:
$project cannot be used to assign new values (to the stage output) to existing fields (in the stage input).


Hi @Vivek_Poddar,

I am a little confused here. Can you post an example for the same?

In case you have any doubts, please feel free to reach out to us.

Thanks and Regards.
Sourabh Bagrecha,
Curriculum Services Engineer

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.