HTML definition


HTML stands for Hyper Text Markup Language , it is a set of markup tags and HTML documents are described by HTML tags , each HTML tag describes different document content .

HTML is a markup language that web browsers use to interpret and compose text, images, and other material into visual or audible web pages. HTML  used to create web pages along with css, and javaScript, as well as to create user interfaces for mibile and web applications.

HTML was a document called "HTML Tags", first mentioned on the Internet by Tim Berners-Lee in late 1991.

It provides a means to create structured documents by denoting structural semantics for text such as headings, paragraphs, lists, links, quotes and other items.


Example:

<html>



  <head>



    <title>This is a title</title>



  </head>



  <body>



    <p>Hello</p>



  </body>



</html>

$sort (aggregation) in MongoDB


$sort is responsible for sorting input document and returns it to the pipeline. If you are aware of relational Databases like SQL server $sort work same as ORDER BY clause.

Syntax:


{ $sort: { <field1>: <sort order>, <field2>: <sort order> ... } }


$sort use to sort result in ascending or descending order according to particular field(s). For ascending and descending we used the following values.

  • 1 for the ascending.
  • -1 for descending

Example:

Suppose we have a collection named “myCollection “as below.


{ "uid" : 1, " Book  Title": "book1", "price" : 20, "quantity" : 1, "date" : ISODate("2016-08-05T07:00:00Z") }
{ "uid" : 2, " Book Title " : " book2", "price" : 10, "quantity" : 2, "date" : ISODate("2016-08-05T08:00:00Z") }
{ "uid" : 3, " Book Title " : " book3", "price" : 30, "quantity" : 4, "date" : ISODate("2016-08-17T10:00:00Z") }
{ "uid" : 4, " Book Title " : " book4", "price" : 10, "quantity" : 2, "date" : ISODate("2014-09-01T11:20:39.736Z") }
{ "uid" : 5, " Book Title " : " book5", "price" : 20, "quantity" : 6, "date" : ISODate("2014-09-04T20:23:13.331Z") }


Execute following query:


db.myCollection.aggregate(
   [
     { $sort : { quantity : -1 } }
   ]
)


Result: Above query returns documents as sorted by quantity descending. 


/* 1 */
{
    "_id" : ObjectId("5767b6e21130fb71878298b2"),
    "uid" : 5,
    " Book Title " : " book5",
    "price" : 20,
    "quantity" : 6,
    "date" : ISODate("2014-09-04T20:23:13.331Z")
}

/* 2 */
{
    "_id" : ObjectId("5767b6e21130fb71878298b0"),
    "uid" : 3,
    " Book Title " : " book3",
    "price" : 30,
    "quantity" : 4,
    "date" : ISODate("2016-08-17T10:00:00.000Z")
}

/* 3 */
{
    "_id" : ObjectId("5767b6e21130fb71878298af"),
    "uid" : 2,
    " Book Title " : " book2",
    "price" : 10,
    "quantity" : 2,
    "date" : ISODate("2016-08-05T08:00:00.000Z")
}

/* 4 */
{
    "_id" : ObjectId("5767b6e21130fb71878298b1"),
    "uid" : 4,
    " Book Title " : " book4",
    "price" : 10,
    "quantity" : 2,
    "date" : ISODate("2014-09-01T11:20:39.736Z")
}

/* 5 */
{
    "_id" : ObjectId("5767b6e21130fb71878298ae"),
    "uid" : 1,
    " Book  Title" : "book1",
    "price" : 20,
    "quantity" : 1,
    "date" : ISODate("2016-08-05T07:00:00.000Z")
}


Same like above query we can apply ascending for the result.


db.myCollection.aggregate(
   [
     { $sort : { quantity : 1 } }
   ]
)



Result:


/* 1 */
{
    "_id" : ObjectId("5767b6e21130fb71878298ae"),
    "uid" : 1,
    " Book  Title" : "book1",
    "price" : 20,
    "quantity" : 1,
    "date" : ISODate("2016-08-05T07:00:00.000Z")
}

/* 2 */
{
    "_id" : ObjectId("5767b6e21130fb71878298af"),
    "uid" : 2,
    " Book Title " : " book2",
    "price" : 10,
    "quantity" : 2,
    "date" : ISODate("2016-08-05T08:00:00.000Z")
}

/* 3 */
{
    "_id" : ObjectId("5767b6e21130fb71878298b1"),
    "uid" : 4,
    " Book Title " : " book4",
    "price" : 10,
    "quantity" : 2,
    "date" : ISODate("2014-09-01T11:20:39.736Z")
}

/* 4 */
{
    "_id" : ObjectId("5767b6e21130fb71878298b0"),
    "uid" : 3,
    " Book Title " : " book3",
    "price" : 30,
    "quantity" : 4,
    "date" : ISODate("2016-08-17T10:00:00.000Z")
}

/* 5 */
{
    "_id" : ObjectId("5767b6e21130fb71878298b2"),
    "uid" : 5,
    " Book Title " : " book5",
    "price" : 20,
    "quantity" : 6,
    "date" : ISODate("2014-09-04T20:23:13.331Z")
}


Note: The $sort stage has a limit of 100 megabytes of RAM.

$group (aggregation) in MongoDB


IF we want to group document by specific expression and want to output for each distinct grouping of the document here we have to use $group. IF you are familiar with Relational Database like SQL Server, It's work the same as the GROUP BY clause.

Output document contains the _id field which contains the distinct group by key also output document contains computed fields which grasp the value of some accumulator expression grouped by the _id.

Syntax:


{ $group: { _id: <expression>, <field1>: { <accumulator1> : <expression1> }, ... }


Accumulator Operator:

$num : It's returns the sum of the numeric value. It can be used in $project also in MongoDB 3.2 version.

$avg  : It returns the average of numeric values. It can be used in $project also in MongoDB 3.2 version.

$first : It's returns a value from the first document for each group.

$last : It's returns a value from the first document for each group.

$max : It's returns the highest expression value for each group. It can be used in $project also in MongoDB 3.2 version.

$min : It's returns the lowest expression value for each group.  It can be used in $project also in MongoDB 3.2 version.

$push : It's returns an array of expression values for each group.

$addToSet : It's returns an array of unique expression values for each group. The order of the array elements is undefined.

$stdDevPop : It's returns the population standard deviation of the input values.

$stdDevSamp : It's returns the sample standard deviation of the input values.

Example :


{ "_id" : 1, "Book Title": "book1", "price" : 20, "quantity" : 1, "date" : ISODate("2016-08-05T07:00:00Z") }
{ "_id" : 2, " Book Title " : " book2", "price" : 10, "quantity" : 2, "date" : ISODate("2016-08-05T08:00:00Z") }
{ "_id" : 3, " Book Title " : " book3", "price" : 30, "quantity" : 4, "date" : ISODate("2016-08-17T10:00:00Z") }
{ "_id" : 4, " Book Title " : " book4", "price" : 10, "quantity" : 2, "date" : ISODate("2014-09-01T11:20:39.736Z") }
{ "_id" : 5, " Book Title " : " book5", "price" : 20, "quantity" : 6, "date" : ISODate("2014-09-04T20:23:13.331Z") }


Group by Month, and Year


db.BookOrder.aggregate(
   [
      {
        $group : {
           _id : { month: { $month: "$date" , year: { $year: "$date" } },
           totalAmount: { $sum: { $multiply: [ "$price", "$quantity" ] } },
           totalQty: { $sum: "$quantity" },
           count: { $sum: 1 }
        }
      }
   ]
)


Result:


{ "_id" : { "month" : 8, "year" : 2016 }, " totalAmount " : 60, " totalQty " : 7, "count" : 3 }
{ "_id" : { "month" : 9, "year" : 2016 }, " totalAmount " : 30, " totalQty " : 8, "count" : 2 }


Note : _id filed is mandatory, except _id all fields are optional and computed using accumulator.

Limitation of Group Stage: $group stage memory can't exceed more than 100 megabytes RAM. If the $group stage exceeds his limit by default gives an error.

Related Posts

What is the Use of isNaN Function in JavaScript? A Comprehensive Explanation for Effective Input Validation

In the world of JavaScript, input validation is a critical aspect of ensuring that user-provided data is processed correctly. One indispensa...