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:$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.有关表达式的详细信息,请参阅表达式。
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:在这些情况下:
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") } ])
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
。
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 }
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") }
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") }