AWS documentdb documentation change
Summary
Added comprehensive documentation for the $setOnInsert operator in Amazon DocumentDB, including description, parameters, MongoDB shell examples, and code examples in Node.js and Python
Security assessment
This change adds standard documentation for a MongoDB operator ($setOnInsert) with no mention of security vulnerabilities, patches, or security incidents. The code examples include standard TLS connection parameters (tls=true, tlsCAFile) which are routine security best practices for database connections, but this is not new security documentation - it's standard connection string formatting for DocumentDB.
Diff
diff --git a/documentdb/latest/developerguide/setOnInsert.md b/documentdb/latest/developerguide/setOnInsert.md index 8b1378917..2079d1d66 100644 --- a//documentdb/latest/developerguide/setOnInsert.md +++ b//documentdb/latest/developerguide/setOnInsert.md @@ -0,0 +1 @@ +[](/pdfs/documentdb/latest/developerguide/developerguide.pdf#setOnInsert "Open PDF") @@ -1,0 +3,156 @@ +[Documentation](/index.html)[Amazon DocumentDB](/documentdb/index.html)[Developer Guide](what-is.html) + +Example (MongoDB Shell)Code examples + +# $setOnInsert + +The `$setOnInsert` operator in Amazon DocumentDB is used to set the value of a field if a document is being inserted, but it has no effect if the document is being updated. + +**Parameters** + + * `field`: The field to be set. + + * `value`: The value to assign to the field. + + + + +## Example (MongoDB Shell) + +The following example demonstrates the usage of the `$setOnInsert` operator in Amazon DocumentDB. It creates a new document if the document does not already exist, but it has no effect if the document is being updated. + +**Create sample documents** + + + db.users.insertOne({ + _id: 1, + name: "John Doe", + age: 30 + }) + +**Query example 1 - Update existing document** + + + db.users.update( + { _id: 1 }, + { + $set: { age: 31 }, + $setOnInsert: { createdAt: new Date() } + }, + { upsert: true } + ) + +**Output 1** + + + { + _id: 1, + name: "John Doe", + age: 31 + } + +The output shows that the document was updated, but the `createdAt` field was **NOT added** since the document already existed. The `$setOnInsert` operator only applies when inserting new documents. + +**Query example 2 - Insert new document (upsert)** + + + db.users.update( + { _id: 2 }, + { + $set: { name: "Jane Smith", age: 25 }, + $setOnInsert: { createdAt: new Date() } + }, + { upsert: true } + ) + +**Output 2** + + + { + _id: 2, + name: "Jane Smith", + age: 25, + createdAt: ISODate("2025-10-31T09:57:52.459Z") + } + } + +## Code examples + +To view a code example for using the `$setOnInsert` command, choose the tab for the language that you want to use: + +Node.js + + + + const { MongoClient } = require('mongodb'); + + async function updateWithSetOnInsert() { + 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 users = db.collection('users'); + + await users.updateOne( + { _id: 1 }, + { + $set: { age: 31 }, + $setOnInsert: { createdAt: new Date() } + }, + { upsert: true } + ); + + const updatedUser = await users.findOne({ _id: 1 }); + console.log(updatedUser); + + await client.close(); + } + + updateWithSetOnInsert(); + +Python + + + + from pymongo import MongoClient + + def update_with_set_on_insert(): + client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false') + db = client['test'] + users = db['users'] + + result = users.update_one( + {'_id': 1}, + { + '$set': {'age': 31}, + '$setOnInsert': {'createdAt': datetime.datetime.now()} + }, + upsert=True + ) + + updated_user = users.find_one({'_id': 1}) + print(updated_user) + + client.close() + + update_with_set_on_insert() + + **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) + +$set + +$slice + +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.