$mul - 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).

$mul

The $mul operator in Amazon DocumentDB is used to multiply the value of a field by a specified number. This can be useful for updating multiple documents atomically and consistently, such as updating flight miles based on a credit card status.

Parameters

  • field: The field to be multiplied.

  • multiplier: The number to multiply the field value by.

Example (MongoDB Shell)

This example demonstrates how to use the $mul operator to double the flight_miles value for all documents where the credit_card field is true.

Create sample documents

db.miles.insertMany([ { "_id": 1, "member_since": new Date("1987-01-01"), "credit_card": false, "flight_miles": [1205, 2560, 880] }, { "_id": 2, "member_since": new Date("1982-01-01"), "credit_card": true, "flight_miles": [2410, 5120, 1780, 5560] }, { "_id": 3, "member_since": new Date("1999-01-01"), "credit_card": true, "flight_miles": [2410, 1760] } ]);

Query example

db.miles.update( { "credit_card": { "$eq": true } }, { "$mul": { "flight_miles.$[]": NumberInt(2) } }, { "multi": true } );

Output

{ "_id" : 1, "member_since" : ISODate("1987-01-01T00:00:00Z"), "credit_card" : false, "flight_miles" : [ 1205, 2560, 880 ] } { "_id" : 2, "member_since" : ISODate("1982-01-01T00:00:00Z"), "credit_card" : true, "flight_miles" : [ 4820, 10240, 3560, 11120 ] } { "_id" : 3, "member_since" : ISODate("1999-01-01T00:00:00Z"), "credit_card" : true, "flight_miles" : [ 4820, 3520 ] }

For the customers that have a credit card, their flight miles have been doubled.

The $[] positional array operator is used to apply the $mul operation to each element in the flight_miles array.

Code examples

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

Node.js
const { MongoClient } = require('mongodb'); async function updateFlightMiles() { 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('miles'); await collection.updateMany( { credit_card: true }, { $mul: { 'flight_miles.$[]': 2 } } ); const documents = await collection.find().toArray(); console.log(documents); await client.close(); } updateFlightMiles();
Python
from pymongo import MongoClient def update_flight_miles(): 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.miles collection.update_many( {'credit_card': True}, {'$mul': {'flight_miles.$[]': 2}} ) documents = list(collection.find()) print(documents) client.close() update_flight_miles()