To sort documents in MongoDB 4, use sort(). To display only a single sorted field, set this to 1.
Let us create a collection containing documents -
> db.demo611.insertOne({"Name":"Chris"});{ "acknowledged" : true, "insertedId" : ObjectId("5e987110f6b89257f5584d83") } > db.demo611.insertOne({"Name":"Adam"});{ "acknowledged" : true, "insertedId" : ObjectId("5e987115f6b89257f5584d84") } > db.demo611.insertOne({"Name":"John"});{ "acknowledged" : true, "insertedId" : ObjectId("5e987118f6b89257f5584d85") } > db.demo611.insertOne({"Name":"Bob"});{ "acknowledged" : true, "insertedId" : ObjectId("5e98711bf6b89257f5584d86") }
Display all the documents in the collection with the help of find() method-
> db.demo611.find(); This will produce the following output: { "_id" : ObjectId("5e987110f6b89257f5584d83"), "Name" : "Chris" } { "_id" : ObjectId("5e987115f6b89257f5584d84"), "Name" : "Adam" } { "_id" : ObjectId("5e987118f6b89257f5584d85"), "Name" : "John" } { "_id" : ObjectId("5e98711bf6b89257f5584d86"), "Name" : "Bob" }
The following is how to sort the documents using MongoDB 4 The query -
> db.demo611.find().sort({Name:1});
will produce the following output&mius;
{ "_id" : ObjectId("5e987115f6b89257f5584d84"), "Name" : "Adam" } { "_id" : ObjectId("5e98711bf6b89257f5584d86"), "Name" : "Bob" } { "_id" : ObjectId("5e987110f6b89257f5584d83"), "Name" : "Chris" } { "_id" : ObjectId("5e987118f6b89257f5584d85"), "Name" : "John" }
The above is the detailed content of How to sort documents and display only a single field in MongoDB 4?. For more information, please follow other related articles on the PHP Chinese website!

This article compares MongoDB and MySQL, contrasting their document-oriented and relational architectures. It analyzes performance in read/write operations and complex queries, highlighting MongoDB's scalability and suitability for unstructured data

This article details MongoDB's Create, Read, Update, and Delete (CRUD) operations. It covers inserting, updating, deleting, and querying data using both the MongoDB shell and drivers, emphasizing efficient querying of large datasets and best practic

This article details MongoDB document field updates using updateOne, updateMany, and findAndModify. It also covers MongoDB's delete operations (deleteOne, deleteMany, findOneAndDelete) and emphasizes robust error handling via try-catch blocks, logg

This article provides a comprehensive guide to MongoDB's CRUD operations (Create, Read, Update, Delete). It details best practices for efficient data handling, including indexing, batch operations, and query optimization, while also addressing chal

This tutorial guides MongoDB installation on Linux, covering prerequisites (OS compatibility, disk space, system requirements, user privileges), configuration (storage engine, memory allocation, journaling, indexes, network settings), and troubleshoo

This article details MongoDB database deletion methods. It focuses on the dropDatabase() and db.dropDatabase() commands, highlighting their irreversible nature and emphasizing the independent nature of databases within MongoDB, preventing accidental

This article examines when MongoDB is the optimal database choice. It highlights MongoDB's strengths in handling unstructured data, scaling efficiently, and enabling rapid development due to its flexible schema. However, it acknowledges that relati

The article discusses creating users and roles in MongoDB, managing permissions, ensuring security, and automating these processes. It emphasizes best practices like least privilege and role-based access control.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download
The most popular open source editor
