$switch - Amazon DocumentDB

$switch

New from version 4.0.

Not supported by Elastic cluster.

The $switch operator is a conditional expression operator in Amazon DocumentDB that allows you to evaluate a list of case expressions and return the value of the first case that evaluates to true, or a default value if no case expression is true.

Parameters

  • branches: An array of documents, each of which has a case field that contains the boolean expression to evaluate, and a then field that contains the value to return if the case expression is true.

  • default: (optional) The value to return if none of the case expressions are true.

Example (MongoDB Shell)

The following example demonstrates the use of the $switch operator to determine the shipping cost for an order based on the order total.

Create sample documents

db.orders.insertMany([ { _id: 1, total: 50 }, { _id: 2, total: 150 }, { _id: 3, total: 250 } ]);

Query example

db.orders.aggregate([ { $project: { _id: 1, total: 1, shippingCost: { $switch: { branches: [ { case: { $lte: ["$total", 100] }, then: 5 }, { case: { $lte: ["$total", 200] }, then: 10 }, { case: { $gt: ["$total", 200] }, then: 15 } ], default: 0 } } } } ])

Output

[ { "_id": 1, "total": 50, "shippingCost": 5 }, { "_id": 2, "total": 150, "shippingCost": 10 }, { "_id": 3, "total": 250, "shippingCost": 15 } ]

Code examples

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

Node.js
const { MongoClient } = require('mongodb'); async function main() { 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('orders'); const result = await collection.aggregate([ { $project: { _id: 1, total: 1, shippingCost: { $switch: { branches: [ { case: { $lte: ['$total', 100] }, then: 5 }, { case: { $lte: ['$total', 200] }, then: 10 }, { case: { $gt: ['$total', 200] }, then: 15 } ], default: 0 } } } } ]).toArray(); console.log(result); await client.close(); } main();
Python
from pymongo import MongoClient def main(): 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.orders result = list(collection.aggregate([ { '$project': { '_id': 1, 'total': 1, 'shippingCost': { '$switch': { 'branches': [ { 'case': { '$lte': ['$total', 100] }, 'then': 5 }, { 'case': { '$lte': ['$total', 200] }, 'then': 10 }, { 'case': { '$gt': ['$total', 200] }, 'then': 15 } ], 'default': 0 } } } } ])) print(result) client.close() if __name__ == '__main__': main()