$addFields - Amazon DocumentDB
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

$addFields

The $addFields stage in the Amazon DocumentDB aggregation pipeline allows you to add new computed fields to documents. This can be useful for adding derived or transformed data to the documents.

Parameters

  • newField: The name of the new field to add.

  • expression: An expression that resolves to the value of the new field.

Example (MongoDB Shell)

The following example demonstrates how to use $addFields to add a new field TotalInventory that calculates the total inventory based on the Inventory.OnHand and Inventory.OrderQnty fields.

Create sample documents

db.example.insertMany([ { "Item": "Spray Paint", "Colors": ["Black", "Red", "Green", "Blue"], "Inventory": { "OnHand": 47, "MinOnHand": 50, "OrderQnty": 36 }, "UnitPrice": 3.99 }, { "Item": "Ruler", "Colors": ["Red", "Green", "Blue", "Clear", "Yellow"], "Inventory": { "OnHand": 47, "MinOnHand": 40 }, "UnitPrice": 0.89 } ]);

Query example

db.example.aggregate([ { $addFields: { TotalInventory: { $add: ["$Inventory.OnHand", "$Inventory.OrderQnty"] } } } ])

Output

[ { "_id" : ObjectId("5bedafbcf65ff161707de24f"), "Item" : "Ruler", "Colors" : [ "Red", "Green", "Blue", "Clear", "Yellow" ], "Inventory" : { "OnHand" : 47, "MinOnHand" : 40 }, "UnitPrice" : 0.89, "TotalInventory" : 47 }, { "_id" : ObjectId("5bedafbcf65ff161707de250"), "Item" : "Spray Paint", "Colors" : [ "Black", "Red", "Green", "Blue" ], "Inventory" : { "OnHand" : 47, "MinOnHand" : 50, "OrderQnty" : 36 }, "UnitPrice" : 3.99, "TotalInventory" : 83 } ]

Code examples

To view a code example for using the $addFields command, choose the tab for the language that you want to use:

Node.js
const { MongoClient } = require('mongodb'); const client = await MongoClient.connect('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false'); const db = client.db('test'); const collection = db.collection('example'); const result = await collection.aggregate([ { $addFields: { TotalInventory: { $add: ['$Inventory.OnHand', '$Inventory.OrderQnty'] } } } ]).toArray(); console.log(result); await client.close();
Python
from pymongo import MongoClient client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false') db = client['test'] collection = db['example'] result = list(collection.aggregate([ { '$addFields': { 'TotalInventory': { '$add': ['$Inventory.OnHand', '$Inventory.OrderQnty'] } } } ])) print(result) client.close()