Get Distinct Values with Sorted Data in MongoDB?


Following is the query to get distinct values with sorted data in MongoDB

db.yourCollectionName.distinct("yourFieldName").sort();

Let us first create a collection with documents

>db.getDistinctWithSortedDataDemo.insertOne({"StudentId":10,"StudentName":"John","StudentAge":23});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c9b1e3315e86fd1496b38c3")
}
>db.getDistinctWithSortedDataDemo.insertOne({"StudentId":20,"StudentName":"Carol","StudentAge":21});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c9b1e3e15e86fd1496b38c4")
}
>db.getDistinctWithSortedDataDemo.insertOne({"StudentId":10,"StudentName":"John","StudentAge":23});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c9b1e4415e86fd1496b38c5")
}
>db.getDistinctWithSortedDataDemo.insertOne({"StudentId":30,"StudentName":"Chris","StudentAge":22});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c9b1e5115e86fd1496b38c6")
}
>db.getDistinctWithSortedDataDemo.insertOne({"StudentId":20,"StudentName":"Carol","StudentAge":21});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c9b1e5715e86fd1496b38c7")
}
>db.getDistinctWithSortedDataDemo.insertOne({"StudentId":40,"StudentName":"Bob","StudentAge":20});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c9b1e6515e86fd1496b38c8")
}
>db.getDistinctWithSortedDataDemo.insertOne({"StudentId":40,"StudentName":"Bob","Stude
ntAge":20});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c9b1e6d15e86fd1496b38c9")
}

Following is the query to display all documents from a collection with the help of find() method;

> db.getDistinctWithSortedDataDemo.find().pretty();

This will produce the following output

{
   "_id" : ObjectId("5c9b1e3315e86fd1496b38c3"),
   "StudentId" : 10,
   "StudentName" : "John",
   "StudentAge" : 23
}
{
   "_id" : ObjectId("5c9b1e3e15e86fd1496b38c4"),
   "StudentId" : 20,
   "StudentName" : "Carol",
   "StudentAge" : 21
}
{
   "_id" : ObjectId("5c9b1e4415e86fd1496b38c5"),
   "StudentId" : 10,
   "StudentName" : "John",
   "StudentAge" : 23
}
{
   "_id" : ObjectId("5c9b1e5115e86fd1496b38c6"),
   "StudentId" : 30,
   "StudentName" : "Chris",
   "StudentAge" : 22
}
{
   "_id" : ObjectId("5c9b1e5715e86fd1496b38c7"),
   "StudentId" : 20,
   "StudentName" : "Carol",
   "StudentAge" : 21
}
{
   "_id" : ObjectId("5c9b1e6515e86fd1496b38c8"),
   "StudentId" : 40,
   "StudentName" : "Bob",
   "StudentAge" : 20
}
{
   "_id" : ObjectId("5c9b1e6d15e86fd1496b38c9"),
   "StudentId" : 40,
   "StudentName" : "Bob",
   "StudentAge" : 20
}

Following is the query to get distinct values with sorted data for “StudentName”

> db.getDistinctWithSortedDataDemo.distinct("StudentName").sort();

This will produce the following output

[ "Bob", "Carol", "Chris", "John" ]

Following is the query to get distinct values with sorted data for StudentAge

> db.getDistinctWithSortedDataDemo.distinct("StudentAge").sort();

This will produce the following output

[ 20, 21, 22, 23 ]

Updated on: 30-Jul-2019

832 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements