$slice - Amazon DocumentDB (original) (raw)
The $slice update operator modifies an array by limiting its size. When used with the $push operator, it restricts the number of elements in an array, keeping only the specified number of most recent or oldest elements.
Parameters
field: The array field to modify.count: Maximum number of elements to keep. Positive values keep the first N elements, negative values keep the last N elements.
Example (MongoDB Shell)
The following example demonstrates how to use the $slice update operator with $push to maintain a fixed-size array of recent scores.
Create sample documents
db.students.insertOne({
_id: 1,
name: "Alice",
scores: [85, 90, 78]
});Query example
db.students.updateOne(
{ _id: 1 },
{
$push: {
scores: {
$each: [92, 88],
$slice: -3
}
}
}
)Output
{
"_id" : 1,
"name" : "Alice",
"scores" : [ 78, 92, 88 ]
}In this example, the $slice: -3 modifier keeps only the last three elements after pushing new values to the array.
Code examples
To view a code example for using the $slice update 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('students');
await collection.updateOne(
{ _id: 1 },
{
$push: {
scores: {
$each: [92, 88],
$slice: -3
}
}
}
);
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.students
collection.update_one(
{'_id': 1},
{
'$push': {
'scores': {
'$each': [92, 88],
'$slice': -3
}
}
}
)
updated_document = collection.find_one({'_id': 1})
print(updated_document)
client.close()
update_document()