$subtract (aggregation)

On this page本页内容

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:$subtract表达式语法如下:

{ $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行为

Starting in MongoDB 5.0, the result will have the same type as the input except when it cannot be represented accurately in that type. 从MongoDB 5.0开始,结果将与输入具有相同的类型,除非无法用该类型准确表示。In these cases:在这些情况下:

  • A 32-bit integer will be converted to a 64-bit integer if the result is representable as a 64-bit integer.如果结果可以表示为64位整数,则32位整数将转换为64位整型。
  • A 32-bit integer will be converted to a double if the result is not representable as a 64-bit integer.如果结果不能表示为64位整数,则32位整数将转换为双精度数。
  • A 64-bit integer will be converted to double if the result is not representable as a 64-bit integer.如果结果不能表示为64位整数,则64位整数将转换为双精度。

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.以下聚合使用$subtract表达式,通过从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 (available starting in 4.2) and returns the difference in milliseconds:以下聚合使用$subtract表达式从当前日期减去$date,使用系统NOW(从4.2开始可用)并返回以毫秒为单位的差值:

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" : NumberLong("186136746187") }
{ "_id" : 2, "item" : "jkl", "dateDifference" : NumberLong("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:以下聚合使用$subtract表达式从"$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") }
←  $substrCP (aggregation)$sum (aggregation) →