MongoDB $subtract 运算符

MongoDB $subtract 运算符 介绍

MongoDB 提供了多种算术表达式运算符。$subtract 运算符就是这些运算符之一。此运算符用于将两个数字或日期相减并返回差值。

  1. 如果参数是两个数字,则结果将以数字形式出现。
  2. 如果参数是两个日期,则结果将以毫秒为单位。
  3. 如果参数是日期和以毫秒为单位的数字,则结果将以日期形式出现。

MongoDB $subtract 运算符 语法

{ $subtract: [ < expression 1 >, < expression 2 > ] }  

MongoDB $subtract 运算符 例子

假设我们有一组具有以下文档的学生。

>db.students.find().pretty()  
{  
         {  
             "_id" : 1,  
             "std_name" : "John",  
             "father_name" : "Mick",  
             "department" : "MCA",  
             "semester_fee" : 6000,  
             "annual_fee" : 10000,  
             "start_date" : ISODate("2019-07-03T08:00:00Z"),  
             "end_date" : ISODate("2021-05-26T09:00:00Z")  
         }  
         {  
             "_id" : 2,  
             "std_name" : "Oliver",  
             "father_name" : "Thomas",  
             "department" : "BCA",  
             "semester_fee" : 4000,  
             "annual_fee" : 6000,  
             "start_date" : ISODate("2020-07-03T08:00:00Z"),  
             "end_date" : ISODate("2023-05-01T09:00:00Z")  
         }  
         {  
             "_id" : 3,  
             "std_name" : "Jack",  
             "father_name" : "James",  
             "department" : "MCA",  
             "semester_fee" : 7000,  
             "annual_fee" : 12500,  
             "start_date" : ISODate("2020-07-11T00:00:00Z"),  
             "end_date" : ISODate("2022-05-25T09:00:00Z")  
         }  
         {  
             "_id" : 4,  
             "std_name" : "Robert",  
             "father_name" : "David",  
             "department" : "Btech",  
             "fees" : {  
                        "semester_fee" : 15000,  
                        "annual_fee" : 22500  
                      }  
             "start_date" : ISODate("2018-07-11T08:00:00Z"),  
             "end_date" : ISODate("2022-05-25T09:00:00Z")  
         }  
         {  
             "_id" : 5,  
             "std_name" : "Richard",  
             "father_name" : "William",  
             "department" : "BCA",  
             "semester_fee" : 11500,  
             "annual_fee" : 20000,  
             "start_date" : ISODate("2020-07-03T08:00:00Z"),  
             "end_date" : ISODate("2023-05-01T09:00:00Z")  
         }  
         {  
             "_id" : 6,  
             "std_name" : "Daniel",  
             "father_name" : "Paul",  
             "department" : "MCA",  
             "semester_fees" : 12500,  
             "annual_fee" : 25000,  
             "start_date" : ISODate("2018-07-11T08:00:00Z"),  
             "end_date" : ISODate("2020-05-25T09:00:00Z")  
         }  
}

示例 1:使用 $subtract 运算符减去两个数字

在此示例中,我们将使用 $subtract 运算符从每年的费用字段中减去学期费用字段。

db.students.aggregate(  
 [  
   {$match: { department : "MCA" }},  
      {  
          $project:   
              {  
                std_name : 1,  
                father_name : 1,  
                semester_fee : 1,  
                annual_fee : 1,  
                result : { $subtract : [ "$annual_fee", "$semester_fee" ] }  
               }  
      }  
 ]  
)  

输出结果为:

{
    "_id" : 1,
    "std_name" : "John",
    "father_name" : "Mick",
    "semester_fee" : 6000,
    "annual_fee" : 10000,
    "result" : 4000
}    
{
    "_id" : 3,
    "std_name" : "Jack",
    "father_name" : "James",
    "semester_fee" : 7000,
    "annual_fee" : 12500,
    "result" : 5500
}
{
    "_id" : 6,
    "std_name" : "Daniel",
    "father_name" : "Paul",
    "semester_fee" : 12500,
    "annual_fee" : 25000,
    "result" : 12500
}

示例 2:使用 $subtract 运算符减去两个日期

在此示例中,我们将使用 $subtract 运算符从“end_date”字段中减去“start_date”字段。

db.students.aggregate(  
 [  
   {$match: { department : "BCA" }},  
      {  
          $project:   
              {  
                std_name : 1,  
                father_name : 1,  
                start_date : 1,  
                end_date : 1,  
                result : { $subtract : [ "$end_date ", "$start_date" ] }  
               }  
      }  
 ]  
)  

输出结果为:

{
      "_id" : 2,
      "std_name" : "Oliver",
      "father_name" : "Thomas",
      "start_date" : ISODate("2020-07-03T08:00:00Z"),
      "end_date" : ISODate("2023-05-01T09:00:00Z"),
      "result" : NumberLong("18644531576187")
}
{
      "_id" : 5,
      "std_name" : "Richard",
      "father_name" : "William",
      "start_date" : ISODate("2019-07-03T08:00:00Z"),
      "end_date" : ISODate("2022-05-01T09:00:00Z"),
      "result" : NumberLong("18644531576187")
}

示例 3:使用 $subtract 运算符从日期中减去一个数字

在此示例中,我们将使用 $subtract 运算符从“start_date”字段中减去 250。

db.students.aggregate(  
 [  
   {$match: { std_name : "Jack" }},  
      {  
          $project:   
              {  
                std_name : 1,  
                father_name : 1,  
                department : 1,  
                start_date : 1,  
                result : { $subtract : [ "$start_date", 250 ] }  
               }  
      }  
 ]  
)  

输出结果为:

{
             "_id" : 3,
             "std_name" : "Jack",
             "father_name" : "James",
             "department" : "MCA",
             "start_date" : ISODate("2020-07-11T00:00:00Z"),
             "result" : ISODate("2020-07-10T23:59:59.750Z") 
}

我们可以看到从“start_date”字段中减去了 250 毫秒。

如果我们改变参数的位置会发生什么?

db.students.aggregate(  
 [  
   {$match: { std_name : "Jack" }},  
      {  
          $project:   
              {  
                std_name : 1,  
                father_name : 1,  
                department : 1,  
                start_date : 1,  
                result : { $subtract : [ 250, "$start_date" ] }  
               }  
      }  
 ]  
) 

输出结果为:

uncaught exception : Error : command failed : {
	"ok": 0,
	"errmsg": "cant $subtract a date from a double",
	"code": 16556,
	"codeName": "Location16556"
} : aggregate failed:
_getErrorWithCode@src/mongo/shell/utils.js : 25 : 13
doassert@src/mongo/shell/assert.js : 18 : 14
_assertCommandWorked@src/mongo/shell/assert.js : 618 : 17
assert.commandWorked@src/mongo/shell/assert.js : 708 : 16
DB.prototype._runAggregate@src/mongo/shell/db.js : 266 : 5
DBCollection.prototype.aggregate@src/mongo/shell/collection.js : 1046 : 12
@(shell) : 1 : 1

如您所见,如果第一个参数是数字而第二个参数是日期,则会出现错误。

热门文章

优秀文章