$set and $addFields
noun
say it: “DOL-er SET and DOL-er ad-FEELDZ” /ˈdɑlər ˈsɛt ənd ˈdɑlər ˌædˈfildz/
Two names for one MongoDB aggregation stage that adds a calculated field to each document while keeping the rest.
What does the $set (or $addFields) stage do?
$set and $addFields are two names for the same aggregation stage: it adds a field worked out for each document, or replaces one with the same name, and keeps every other field. (In an update, $set is the update operator instead.)
db.orders.aggregate([
{ $set: { totalWithTax: { $multiply: ["$total", 1.07] } } }
]);
// each order with all its fields, plus totalWithTax
In a sentence
- “A $set and $addFields stage put a lineTotal on every order item.”
- “Unlike $project, $set and $addFields keep every field the document already had.”
Level: Journeyman · More MongoDB terms at this level
Updated