Write mongoDB query for sorting documents in descending and asceding order based on _id field.
Collection Name: movies
[
{
_id: ObjectId('602e693f7e8df9001191c1a1'),
title: 'Inception',
release_year: 2010,
director: 'Christopher Nolan',
genres: [ 'Action', 'Adventure', 'Sci-Fi' ],
cast: [ 'Leonardo DiCaprio', 'Joseph Gordon-Levitt', 'Ellen Page' ],
plot: 'A thief who enters the dreams of others to steal their secrets from their subconscious.',
ratings: { imdb: 8.8, rotten_tomatoes: 87, metacritic: 74 },
box_office: { budget: 160000000, gross: 829895144 }
},
{
_id: ObjectId('602e693f7e8df9001191c1a2'),
title: 'The Dark Knight',
release_year: 2008,
director: 'Christopher Nolan',
genres: [ 'Action', 'Crime', 'Drama' ],
cast: [ 'Christian Bale', 'Heath Ledger', 'Aaron Eckhart' ],
plot: 'When the menace known as The Joker emerges from his mysterious past, he wreaks havoc and chaos on the people of Gotham.',
ratings: { imdb: 9, rotten_tomatoes: 94, metacritic: 84 },
box_office: { budget: 185000000, gross: 1004558444 }
},
{
_id: ObjectId('602e693f7e8df9001191c1a3'),
title: 'The Shawshank Redemption',
release_year: 1994,
director: 'Frank Darabont',
genres: [ 'Drama' ],
cast: [ 'Tim Robbins', 'Morgan Freeman', 'Bob Gunton' ],
plot: 'Two imprisoned men bond over a number of years, finding solace and eventual redemption through acts of common decency.',
ratings: { imdb: 9.3, rotten_tomatoes: 91, metacritic: 80 },
box_office: { budget: 25000000, gross: 58700000 }
},
{
_id: ObjectId('602e693f7e8df9001191c1a4'),
title: 'Pulp Fiction',
release_year: 1994,
director: 'Quentin Tarantino',
genres: [ 'Crime', 'Drama' ],
cast: [ 'John Travolta', 'Uma Thurman', 'Samuel L. Jackson' ],
plot: 'The lives of two mob hitmen, a boxer, a gangster and his wife, and a pair of diner bandits intertwine in four tales of violence and redemption.',
ratings: { imdb: 8.9, rotten_tomatoes: 92, metacritic: 94 },
box_office: { budget: 8000000, gross: 213928762 }
},
{
_id: ObjectId('602e693f7e8df9001191c1a5'),
title: 'The Godfather',
release_year: 1972,
director: 'Francis Ford Coppola',
genres: [ 'Crime', 'Drama' ],
cast: [ 'Marlon Brando', 'Al Pacino', 'James Caan' ],
plot: 'The aging patriarch of an organized crime dynasty transfers control of his clandestine empire to his reluctant son.',
ratings: { imdb: 9.2, rotten_tomatoes: 98, metacritic: 100 },
box_office: { budget: 6000000, gross: 245066411 }
}
]
To learn more about ObjectId see : All about ObjectId() in MongoDB
db.movies.find({}).sort({_id:-1})
db.movies.find({}).sort({_id:1})