$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: