Add Field formula

Hello,

In chapter 1 > lecture part 3 (Bar Chart) I was quite positively surprised it was possible to create a formula such as:

price * (365 - availability.availability_365)

Coming from course M121 (Aggregations) I understand this option is equivalent to $addField. However, in an aggregation pipeline, such a simplfied syntax doesn’t work. Instead, we should write:

$multiply: [
  "$price", {$subtract: [
    365, "$availability.availability_365"]
    }
  ]

Could you explain why the simplied syntax works in Charts whereas it does not in Aggregations, please?

Hi @Antoine_Delequeuche, welcome back to the community.

Actually, that simplified syntax is a MongoDB Charts feature provided as a convenience for math expressions, that eventually translates it into a MongoDB Aggregation expression.
Simple expressions do not follow the conventions of JSON-like documents or support the full range of aggregation operators/expressions, so they are not part of the MongoDB Query Language.

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

Thanks and Regards.
Sourabh Bagrecha,
Curriculum Services Engineer

1 Like

Hello @SourabhBagrecha

Thank you for taking the time to answer me. Your answer is clear, I understand. I would be a nice addition to the course to specify such amenities: when one is used to MQL, there is a limiting belief that MQL syntax must be respected at all times.

Hi @Antoine_Delequeuche, thanks for the feedback I will forward this to the appropriate team.

Thanks and Regards.
Sourabh Bagrecha,
Curriculum Services Engineer