$sort - Amazon DocumentDB
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).

$sort

The $sort update modifier orders array elements when used with the $push operator. It arranges array elements in ascending or descending order based on specified field values or the elements themselves.

Parameters

  • field: The array field to modify.

  • order: Use 1 for ascending order or -1 for descending order.

Example (MongoDB Shell)

The following example demonstrates using the $sort modifier with $push to add new quiz scores and keep them sorted in descending order.

Create sample documents

db.students.insertOne({ _id: 1, name: "Bob", quizzes: [ { score: 85, date: "2024-01-15" }, { score: 92, date: "2024-02-10" } ] });

Query example

db.students.updateOne( { _id: 1 }, { $push: { quizzes: { $each: [{ score: 78, date: "2024-03-05" }], $sort: { score: -1 } } } } )

Output

{ "_id" : 1, "name" : "Bob", "quizzes" : [ { "score" : 92, "date" : "2024-02-10" }, { "score" : 85, "date" : "2024-01-15" }, { "score" : 78, "date" : "2024-03-05" } ] }

Code examples

To view a code example for using the $sort update modifier, 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: { quizzes: { $each: [{ score: 78, date: "2024-03-05" }], $sort: { score: -1 } } } } ); 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': { 'quizzes': { '$each': [{'score': 78, 'date': '2024-03-05'}], '$sort': {'score': -1} } } } ) updated_document = collection.find_one({'_id': 1}) print(updated_document) client.close() update_document()