Docs HomeMongoDB Manual

$subtract (aggregation)

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. In these cases:从MongoDB 5.0开始,结果将具有与输入相同的类型,除非无法在该类型中准确表示。在这些情况下:

  • 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表达式,使用系统NOW(从4.2开始可用)从当前日期减去$date,并返回以毫秒为单位的差值:

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

Alternatively, you can use the Date() for the current date:s或者,您可以将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") }