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).
$[]
The $[] all positional operator updates all elements in an array. It is used when you need to modify every element in an array field.
Parameters
Example (MongoDB Shell)
The following example demonstrates using the $[] operator to update all array elements.
Create sample documents
db.products.insertOne({
_id: 1,
name: "Laptop",
prices: [1000, 1100, 1200]
});
Query example
db.products.updateOne(
{ _id: 1 },
{ $inc: { "prices.$[]": 50 } }
);
Output
{
"_id" : 1,
"name" : "Laptop",
"prices" : [ 1050, 1150, 1250 ]
}
Code examples
To view a code example for using the $[] operator, choose the tab for the language that you want to use:
- Node.js
-
const { MongoClient } = require('mongodb');
async function updateDocument() {
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('products');
await collection.updateOne(
{ _id: 1 },
{ $inc: { "prices.$[]": 50 } }
);
const updatedDocument = await collection.findOne({ _id: 1 });
console.log(updatedDocument);
await client.close();
}
updateDocument();
- Python
-
from pymongo import MongoClient
def update_document():
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.products
collection.update_one(
{'_id': 1},
{'$inc': {'prices.$[]': 50}}
)
updated_document = collection.find_one({'_id': 1})
print(updated_document)
client.close()
update_document()