$natural - 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).

$natural

The $natural operator in Amazon DocumentDB is used to sort the documents in their natural order, which is the order in which they were inserted into the collection. This is in contrast to the default sorting behavior, which is to sort the documents based on the values of the specified fields.

Parameters

None

Example (MongoDB Shell)

The following example demonstrates how to use the $natural operator to sort the documents in a collection in their natural order.

Create sample documents

db.people.insertMany([ { "_id": 1, "name": "María García", "age": 28 }, { "_id": 2, "name": "Arnav Desai", "age": 32 }, { "_id": 3, "name": "Li Juan", "age": 25 }, { "_id": 4, "name": "Carlos Salazar", "age": 41 }, { "_id": 5, "name": "Sofia Martínez", "age": 35 } ]);

Query example

db.people.find({}, { "_id": 1, "name": 1 }).sort({ "$natural": 1 });

Output

[ { "_id": 1, "name": "María García" }, { "_id": 2, "name": "Arnav Desai" }, { "_id": 3, "name": "Li Juan" }, { "_id": 4, "name": "Carlos Salazar" }, { "_id": 5, "name": "Sofia Martínez" } ]

The query sorts the documents in the collection in their natural order, which is the order in which they were inserted.

Code examples

To view a code example for using the $natural 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('people'); const documents = await collection.find({}, { projection: { _id: 1, name: 1 } }) .sort({ $natural: 1 }) .toArray(); console.log(documents); 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['people'] documents = list(collection.find({}, {'_id': 1, 'name': 1}).sort('$natural', 1)) print(documents) client.close() example()