$slice - Amazon DocumentDB

$slice

The $slice aggregation operator enables you to return a subset of an array by either traversing the array from the beginning or the end of the array. This is used to display a limited number of items from an array field, such as the top or bottom N items.

Parameters

  • array: The array field to be sliced.

  • n: An integer that specifies the number of elements to return. A positive value starts from the beginning of the array, while a negative value starts from the end of the array.

Example (MongoDB Shell)

The following example demonstrates how to use $slice to return the first two favorite sweets for each chef.

Create sample documents

db.sweets.insertMany([ { "_id" : 1, "name" : "Alvin", "favorites": [ "chocolate", "cake", "toffee", "beignets" ] }, { "_id" : 2, "name" : "Tom", "favorites": [ "donuts", "pudding", "pie" ] }, { "_id" : 3, "name" : "Jessica", "favorites": [ "fudge", "smores", "pudding", "cupcakes" ] }, { "_id" : 4, "name" : "Rachel", "favorites": [ "ice cream" ] } ]);

Query example

db.sweets.aggregate([ { $project: { _id: 0, name: 1, topTwoFavorites: { $slice: [ "$favorites", 2 ] } } } ]);

Output

[ { name: 'Alvin', topTwoFavorites: [ 'chocolate', 'cake' ] }, { name: 'Tom', topTwoFavorites: [ 'donuts', 'pudding' ] }, { name: 'Jessica', topTwoFavorites: [ 'fudge', 'smores' ] }, { name: 'Rachel', topTwoFavorites: [ 'ice cream' ] } ]

In this example, the $slice operator is used to extract the first two elements from the favorites array for each document.

Code examples

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

Node.js
const { MongoClient } = require('mongodb'); async function example() { 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('sweets'); const result = await collection.aggregate([ { $project: { name: 1, topTwoFavorites: { $slice: ['$favorites', 2] } } } ]).toArray(); console.log(result); await client.close(); } example();
Python
from pymongo import MongoClient def example(): 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['sweets'] result = list(collection.aggregate([ { '$project': { 'name': 1, 'topTwoFavorites': { '$slice': ['$favorites', 2] } } } ])) print(result) client.close() example()