Database Manual / Reference / Query Language / Expressions

$subtract (expression operator)(表达式运算符)

Definition定义

$subtract

Subtracts two numbers to return the difference, or two dates to return the difference in milliseconds, or a date and a number in milliseconds to return the resulting date.减去两个数字以返回差值,或减去两个日期以毫秒为单位返回差值,或者减去一个日期和一个数字以毫秒为单元返回结果日期。

The $subtract expression has the following syntax:$subject表达式具有以下语法:

{ $subtract: [ <expression1>, <expression2> ] }

The second argument is subtracted from the first argument.从第一个参数中减去第二个参数。

The arguments can be any valid expression as long as they resolve to numbers and/or dates. To subtract a number from a date, the date must be the first argument. 参数可以是任何有效的表达式,只要它们解析为数字和/或日期。要从日期中减去数字,日期必须是第一个参数。For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式

Behavior行为

When input types are mixed, $subtract promotes the smaller input type to the larger of the two. A type is considered larger when it represents a wider range of values. The order of numeric types from smallest to largest is: 当输入类型混合时,$subject会将较小的输入类型升级为两者中较大的输入类型。当一个类型代表更宽的值范围时,它被认为更大。数字类型从最小到最大的顺序是:integer → long → double → decimal

The larger of the input types also determines the result type unless the operation overflows and is beyond the range represented by that larger data type. In cases of overflow, $subtract promotes the result according to the following order:较大的输入类型也决定了结果类型,除非操作溢出并且超出了较大数据类型表示的范围。在溢出的情况下,$subject会按照以下顺序提升结果:

  • If the larger input type is integer, the result type is promoted to long.如果较大的输入类型是integer,则结果类型将提升为long
  • If the larger input type is long, the result type is promoted to double.如果较大的输入类型为long,则结果类型将提升为double
  • If the larger type is double or decimal, the overflow result is represented as + or - infinity. There is no type promotion of the result.如果较大的类型是doubledecimal,则溢出结果表示为+或-无穷大。结果没有类型推广。

When mixing Date and non-integer operands, $subtract rounds the non-integer value to the nearest integer before performing the operation.当混合Date和非整数操作数时,$subject会在执行操作之前将非整数值四舍五入到最接近的整数。

Examples示例

Consider a sales collection with the following documents:考虑一个包含以下文件的sales集合:

db.sales.insertMany([
{ "_id" : 1, "item" : "abc", "price" : 10, "fee" : 2, "discount" : 5, "date" : ISODate("2014-03-01T08:00:00Z") },
{ "_id" : 2, "item" : "jkl", "price" : 20, "fee" : 1, "discount" : 2, "date" : ISODate("2014-03-01T09:00:00Z") }
])

Subtract Numbers减去数字

The following aggregation uses the $subtract expression to compute the total by subtracting the discount from the subtotal of price and fee.以下聚合使用$subject表达式通过从pricefee的小计中减去discount来计算total

db.sales.aggregate( [ { $project: { item: 1, total: { $subtract: [ { $add: [ "$price", "$fee" ] }, "$discount" ] } } } ] )

The operation returns the following results:该操作返回以下结果:

{ "_id" : 1, "item" : "abc", "total" : 7 }
{ "_id" : 2, "item" : "jkl", "total" : 19 }

Subtract Two Dates两个日期相减

The following aggregation uses the $subtract expression to subtract $date from the current date, using the system NOW and returns the difference in milliseconds:以下聚合使用$subject表达式从当前日期中减去$date,使用系统NOW并返回以毫秒为单位的差值:

db.sales.aggregate( [ { $project: { item: 1, dateDifference: { $subtract: [ "$$NOW", "$date" ] } } } ] )

Alternatively, you can use the Date() for the current date:或者,您可以使用Date()表示当前日期:

db.sales.aggregate( [ { $project: { item: 1, dateDifference: { $subtract: [ new Date(), "$date" ] } } } ] )

Both operations return documents that resemble the following:这两个操作都返回类似以下内容的文档:

{ "_id" : 1, "item" : "abc", "dateDifference" : Long("186136746187") }
{ "_id" : 2, "item" : "jkl", "dateDifference" : Long("186133146187") }

Subtract Milliseconds from a Date从日期中减去毫秒

The following aggregation uses the $subtract expression to subtract 5 * 60 * 1000 milliseconds (5 minutes) from the "$date" field:以下聚合使用$subject表达式从“$date”字段中减去5*60*1000毫秒(5分钟):

db.sales.aggregate( [ { $project: { item: 1, dateDifference: { $subtract: [ "$date", 5 * 60 * 1000 ] } } } ] )

The operation returns the following results:该操作返回以下结果:

{ "_id" : 1, "item" : "abc", "dateDifference" : ISODate("2014-03-01T07:55:00Z") }
{ "_id" : 2, "item" : "jkl", "dateDifference" : ISODate("2014-03-01T08:55:00Z") }