Home » MongoDB $size Operator

MongoDB $size Operator

by Online Tutorials Library

MongoDB $size Operator

What is the $size operator in MongoDB?

MongoDB provides a variety of array expression operators, and the $size operator is one of those operators. The $size operator is used to find the total number of elements present in the given array, and it is also used in the aggregation pipeline stages.

Syntax of the $size operator:

Important points:

  1. If the argument is missing, then it gives the error.
  2. If the argument does not resolve to an array, then it gives the error.

Examples:

In the following examples, we are working with:

>db.students.find().pretty()  {     "_id" : 1,     "name" : "Jonny",     "class" : "X",     "rollNo" : 401,     "age" : 18,     "marks" : [ 55, 60, 70, 45, 95, 68 ],     "extraMarks" : {                      "practical" : [ 21, 18, 25, 30 ],                      "attendance" : [ 5, 9 ]                    }     "gender" : "Male",     "bloodgroup" : "A+"  }  {     "_id" : 2,     "name" : "Carry",     "class" : "IX",     "rollNo" : 35,     "age" : 17,     "marks" : [ 85, 40, 90, 75, 85, 77 ],     "gender" : "Male",     "bloodgroup" : "B+"  }  {     "_id" : 3,     "name" : "Jin",     "class" : "IX",     "rollNo" : 49,     "age" : 17,     "marks" : [ 85, 70, 80, 95, 94, 81 ],     "gender" : "Female",     "bloodgroup" : "O+"  }  {     "_id" : 4,     "name" : "Thomas",     "class" : "X",     "rollNo" : 61,     "age" : 18,     "marks" : [ 91, 65, 71, 63, 98, 76 ],     "extraMarks" : {                      "practical" : [ 26, 28, 25, 29 ],                      "attendance" : [ 8, 8 ]                    }     "gender" : "Male",     "bloodgroup" : "A+"  }  {     "_id" : 5,     "name" : "Mia",     "class" : "IX",     "rollNo" : 308,     "age" : 17,     "marks" : [ 97, 98, 95, 98 ],     "gender" : "Female",     "bloodgroup" : "B+"  }  {     "_id" : 6,     "name" : "Oats",     "class" : "IX",     "rollNo" : 75,     "age" : 18,     "marks" : [ 99, 98, 98, 95, 96 ],     "gender" : "Male",     "bloodgroup" : "A+"  }  

Example 1: Using $size operator

In this example, we are going to find the total size of the “marks” field using the $size operator.

Output:

{     "name" : "Carry",     "class" : "IX",     "rollNo" : 35,     "marks" : [ 85, 40, 90, 75, 85, 77 ],     "gender" : "Male",     "markssize" : 6  }  {     "name" : "Jin",     "class" : "IX",     "rollNo" : 49,     "marks" : [ 85, 70, 80, 95, 94, 81 ],     "gender" : "Female",     "markssize" : 6  }  {     "name" : "Mia",     "class" : "IX",     "rollNo" : 308,     "marks" : [ 97, 98, 95, 98 ],     "gender" : "Female",     "markssize" : 4  }  {     "name" : "Oats",     "class" : "IX",     "rollNo" : 75,     "marks" : [ 99, 98, 98, 95, 96 ],     "gender" : "Male",     "markssize" : 5  }  

Example 2: Using $size operator in the embedded document

In this example, we are going to find the total size of the extraMarks.practical field using the $size operator.

Output:

{     "name" : "Jonny",     "class" : "X",     "rollNo" : 401,     "marks" : [ 55, 60, 70, 45, 95, 68 ],     "extraMarks" : {                      "practical" : [ 21, 18, 25, 30 ],                      "attendance" : [ 5, 9 ]                    }     "gender" : "Male",     "result" : 4  }  {     "name" : "Thomas",     "class" : "X",     "rollNo" : 61,     "marks" : [ 91, 65, 71, 63, 98, 76 ],     "extraMarks" : {                      "practical" : [ 26, 28, 25, 29 ],                      "attendance" : [ 8, 8 ]                    }     "gender" : "Male",     "result" : 4  }  

You may also like