How does MongoDB order their docs in one collection?


MongoDB order the docs in one collection with the help of a $natural operator. It stores the document as it is when we get from find(). The default order is $natural. Let us now see the syntax −

db.yourCollectionName.find().sort({ "$natural": 1 });

To understand the above syntax, let us create a collection with the document. The query to create a collection with a document is as follows −

> db.orderDocsDemo.insertOne({"UserScore":87});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c9531a316f542d757e2b44b")
}
> db.orderDocsDemo.insertOne({"UserScore":98});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c9531a816f542d757e2b44c")
}
> db.orderDocsDemo.insertOne({"UserScore":99});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c9531b216f542d757e2b44d")
}
> db.orderDocsDemo.insertOne({"UserScore":67});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c9531b716f542d757e2b44e")
}
> db.orderDocsDemo.insertOne({"UserScore":78});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c9531bd16f542d757e2b44f")
}
> db.orderDocsDemo.insertOne({"UserScore":91});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c9531c416f542d757e2b450")
}
> db.orderDocsDemo.insertOne({"UserScore":86});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c9531c816f542d757e2b451")
}

Display all documents from a collection with the help of find() method. The query is as follows −

> db.orderDocsDemo.find();

The following is the output −

{ "_id" : ObjectId("5c9531a316f542d757e2b44b"), "UserScore" : 87 }
{ "_id" : ObjectId("5c9531a816f542d757e2b44c"), "UserScore" : 98 }
{ "_id" : ObjectId("5c9531b216f542d757e2b44d"), "UserScore" : 99 }
{ "_id" : ObjectId("5c9531b716f542d757e2b44e"), "UserScore" : 67 }
{ "_id" : ObjectId("5c9531bd16f542d757e2b44f"), "UserScore" : 78 }
{ "_id" : ObjectId("5c9531c416f542d757e2b450"), "UserScore" : 91 }
{ "_id" : ObjectId("5c9531c816f542d757e2b451"), "UserScore" : 86 }

Here is the query to order the docs in one collection −

> db.orderDocsDemo.find().sort({ "$natural": 1 });

The following is the output −

{ "_id" : ObjectId("5c9531a316f542d757e2b44b"), "UserScore" : 87 }
{ "_id" : ObjectId("5c9531a816f542d757e2b44c"), "UserScore" : 98 }
{ "_id" : ObjectId("5c9531b216f542d757e2b44d"), "UserScore" : 99 }
{ "_id" : ObjectId("5c9531b716f542d757e2b44e"), "UserScore" : 67 }
{ "_id" : ObjectId("5c9531bd16f542d757e2b44f"), "UserScore" : 78 }
{ "_id" : ObjectId("5c9531c416f542d757e2b450"), "UserScore" : 91 }
{ "_id" : ObjectId("5c9531c816f542d757e2b451"), "UserScore" : 86 }

Look at the above sample output, we are getting the same docs in the way we inserted.

In order to get the sorted document, use the following query −

> db.orderDocsDemo.find().sort({ "UserScore": 1 });

The following is the output −

{ "_id" : ObjectId("5c9531b716f542d757e2b44e"), "UserScore" : 67 }
{ "_id" : ObjectId("5c9531bd16f542d757e2b44f"), "UserScore" : 78 }
{ "_id" : ObjectId("5c9531c816f542d757e2b451"), "UserScore" : 86 }
{ "_id" : ObjectId("5c9531a316f542d757e2b44b"), "UserScore" : 87 }
{ "_id" : ObjectId("5c9531c416f542d757e2b450"), "UserScore" : 91 }
{ "_id" : ObjectId("5c9531a816f542d757e2b44c"), "UserScore" : 98 }
{ "_id" : ObjectId("5c9531b216f542d757e2b44d"), "UserScore" : 99 }

Updated on: 30-Jul-2019

45 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements