Linchakin

22nd Aug - MongoDB $abs Operator

 August 22, 2021     No comments   

MongoDB provides a variety of arithmetic expression operators. The $abs operator is one of those operators. The $abs operator is used to return the absolute value of a specific number in the code. Absolute value means that it always returns a positive value whether the number is positive or negative.

Syntax:

Important points:

  1. If the number is null, the $abs operator returns null.
  2. If the entered value refers to a missing field, the $abs operator returns null.
  3. If the number is NaN, the $abs operator returns NaN.

Examples:

In the following examples, we are working with

{
{
"_id" : ObjectId("56254d4fdf2222265r4g12ds3d65f"),
"std_name" : "Micky",
"gender" : "Female",
"class" : "X",
"fees" : 5000,
"exam_fees" : 500,
"age" : 16,
"Total_marks" : 405
"Result" : "Pass"
},
{
"_id" : ObjectId("56254d4fdf2222265r4g12ds34563"),
"std_name" : "Moty",
"gender" : "Male",
"fees" : 4000,
"exam_fees" : 500,
"class" : "VII",
"age" : 15,
"Total_marks" : 705
"Result" : "Pass"
},
{
"_id" : ObjectId("56254d4fdf2222265r4g12ds31478"),
"std_name" : "Thomas",
"gender" : "Male",
"fees" : 3000,
"exam_fees" : 500,
"class" : "V",
"age" : 12,
" Total_marks" : 450
" Result" : "pass"
},
{
"_id" : ObjectId("56254d4fdf2222265r4g12ds37832"),
"std_name" : "Jin",
"gender" : "Female",
"fees" : 5000,
"exam_fees" : 500,
"class" : "X",
"age" : 16,
"Total_marks" : 750
"Result" : "Pass"
},
{
"_id" : ObjectId("56254d4fdf2222265r4g12ds1c46"),
"std_name" : "Mia",
"gender" : "Female",
"fees" : 6000,
"exam_fees" : 500,
"class" : "XI",
"age" : 17,
" Total_marks" : 450
"Result" : "Pass"
},
{
"_id" : ObjectId("56254d4fdf2222265r4g12ds315hj"),
"std_name" : "Mike,
"gender" : "Male",
"fees" : {
"school_fees" :4000,
"exam_fees" : 500,
"pending_fees" : 950,
}
"class" : "V",
"age" : 15,
"Total_marks" : 450
"Result" : "Pass"
}
}

Example 1: Using $abs operator

In this example, we are going to find the total fees (fees + exam_fees) of female student only.

Output:

{
"_id" : ObjectId("56254d4fdf2222265r4g12ds3d65f"),
"std_name" : "Micky",
"class" : "X",
"age" : 16,
"Result" : "Pass",
"Total_fees" : 5500
}
{
"_id" : ObjectId("56254d4fdf2222265r4g12ds37832"),
"std_name" : "Jin",
"class" : "X",
"age" : 16,
"Result" : "Pass",
"Total_fees" : 5500
}
{
"_id" : ObjectId("56254d4fdf2222265r4g12ds1c46"),
"std_name" : "Mia",
"class" : "XI",
"age" : 17,
"Result" : "Pass",
"Total_fees" : 6500,
}

Example 2: MongoDB $abs Operator (retrieving the data in the embedded document)

In this example, we are going to find the total fees (school_fees + exam_fees + pending_fees) of the student whose name is Mike.

Output:

{
"_id" : ObjectId("56254d4fdf2222265r4g12ds315hj"),
"std_name" : "Mike,
"class" : "V",
"age" : 15,
"Result" : "Pass",
"Total_fees" : 5450
}

Next Topic#

Adblock test (Why?)


You may be interested in:
>> Is a Chromebook worth replacing a Windows laptop?
>> Find out in detail the outstanding features of Google Pixel 4a
>> Top 7 best earbuds you should not miss

Related Posts:
>> Recognizing 12 Basic Body Shapes To Choose Better Clothes
>>Ranking the 10 most used smart technology devices
>> Top 5+ Best E-readers: Compact & Convenient Pen
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to XShare to Facebook

Related Posts:

  • …Caramelized Onion Parmesan CrepeThis delicious recipe is easy to make, all you need is some time.  When sautéed over a low heat, the thinly sliced onions caramelize and develop … Read More
  • …Cucina (Calgary)Overall - 7.5/10.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.Food - 7/10Service - 8/10Ambiance -… Read More
  • …Chorizo and Bell Pepper PastaI'll admit that I quite enjoy my pasta loaded up with sauce. However every so  often I do like my pasta simple. In this recipe, the spaghetti is … Read More
  • …Koreana BBQ Restaurant (Calgary)Overall - 8.5/10.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.Food - 9/10Service - 7.5/10Ambiance… Read More
  • Japan (Feb 2014) Day 1 and 2 KyotoDay 1 Singapore to Osaka's Kansai Airport. Stay at KyotoFor day 1,  it was like 9pm+ when we arrived at Kansai Airport. We had to dash to the cou… Read More
Newer Post Older Post Home

0 Comments:

Post a Comment


Copyright © 2025 Linchakin | Powered by Blogger
Design by Hardeep Asrani | Blogger Theme by NewBloggerThemes.com | Distributed By Gooyaabi Templates