On this page本页内容
$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:语法如下所示:
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.有关表达式的详细信息,请参阅表达式。
Consider a 考虑一个sales collection with the following documents:sales集合带有以下文档:
The following aggregation uses the 以下聚合使用$subtract expression to compute the total by subtracting the discount from the subtotal of price and fee.$subtract表达式,通过从price和fee小计中减去discount来计算total。
The operation returns the following results:操作返回以下结果:
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开始提供)并以毫秒为单位返回差值:
Alternatively, you can use the 或者,您可以将Date() for the current date:Date()用于当前日期:
Both operations return documents that resemble the following:这两个操作返回的文档类似于以下内容: