MongoDB $abs 操作符

MongoDB $abs 操作符

MongoDB 提供了各种算术表达式操作符。$abs 操作符是其中之一。$abs 操作符用于返回代码中特定数字的绝对值。绝对值意味着无论数字是正数还是负数,它总是返回一个正值。

语法

{ $abs:  }

重要提示:

  1. 如果数字为空,则$abs运算符返回空。
  2. 如果输入的值指向一个缺失的字段,则$abs运算符返回空。
  3. 如果数字为NaN,则$abs运算符返回NaN。

示例

在以下示例中,我们正在处理

Database: JavaTpoint
Collection: student
Document: Six documents that contain the details of the students
{
        {
                "_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"
        }
}   

示例1:使用$abs运算符

在这个示例中,我们将只计算女学生的总费用(费用+考试费)。

db.student.aggregate(
 [
   {match: {gender : "Female"}},
      {project: 
              {
                name : 1,
                class : 1,
                age : 1,
                Result : 1, 
                Total_fees: {abs: {add: ["fees", "exam_fees"]}}
               }
      }
 ]
)

输出:

{
                "_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,
}

示例2:MongoDB $abs 运算符(检索嵌入文档中的数据)

在这个示例中,我们将找出名为Mike的学生的总费用(学费+考试费+待付费)。

db.student.aggregate(
[
   {match : {name : "Mike"}},
         {project : 
                    { 
                        name : 1, 
                        class : 1,
                        age : 1,
                        Result : 1,
                        Total_fees : {abs : {add : ["fees.school_fees",                                                                      "fees.exam_fees", 
                                                                      "$fees.pending_fees" ]
                                                         }
                                              }
                     }
          }
 ]
)

输出:

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

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程