AWS Security ChangesHomeSearch

AWS documentdb documentation change

Service: documentdb · 2026-03-31 · Documentation low

File: documentdb/latest/developerguide/switch.md

Summary

Added comprehensive documentation for the $switch operator in Amazon DocumentDB, including syntax, parameters, MongoDB Shell example, and code examples in Node.js and Python

Security assessment

This change adds documentation for a new query operator ($switch) in Amazon DocumentDB. There is no evidence of security vulnerability fixes, patches, or security incidents. The documentation includes standard connection strings with TLS parameters (tls=true, tlsCAFile=global-bundle.pem) which are standard security practices for DocumentDB connections, but these are not new security features - they are existing security configurations. The change appears to be routine feature documentation addition.

Diff

diff --git a/documentdb/latest/developerguide/switch.md b/documentdb/latest/developerguide/switch.md
index 8b1378917..44423c766 100644
--- a//documentdb/latest/developerguide/switch.md
+++ b//documentdb/latest/developerguide/switch.md
@@ -0,0 +1 @@
+[](/pdfs/documentdb/latest/developerguide/developerguide.pdf#switch "Open PDF")
@@ -1,0 +3,174 @@
+[Documentation](/index.html)[Amazon DocumentDB](/documentdb/index.html)[Developer Guide](what-is.html)
+
+Example (MongoDB Shell)Code examples
+
+# $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()
+
+![Warning](https://d1ge0kk1l5kms0.cloudfront.net/images/G/01/webservices/console/warning.png) **Javascript is disabled or is unavailable in your browser.**
+
+To use the Amazon Web Services Documentation, Javascript must be enabled. Please refer to your browser's Help pages for instructions.
+
+[Document Conventions](/general/latest/gr/docconventions.html)
+
+$sum
+
+$toBool
+
+Did this page help you? - Yes
+
+Thanks for letting us know we're doing a good job!
+
+If you've got a moment, please tell us what we did right so we can do more of it.
+
+Did this page help you? - No
+
+Thanks for letting us know this page needs work. We're sorry we let you down.
+
+If you've got a moment, please tell us how we can make the documentation better.