I’d love to create a question in metabase using MongoDB as backend. I do have a document with the following structure: Show
Now like to do MongoDB provides a variety of arithmetic expression operators. The $subtract operator is one of those operators. This operator is used to subtract two numbers or dates and returns the difference.
Syntax of the $subtract operator:Note: The arguments must be a valid expression, such as a number or a date. In this operator, the second argument is subtracted by the first argument. If you want to subtract a number from a date, the first argument should always be a date.Examples:Suppose we have a collection of the students with the following documents. Example 1: Subtract two numbers using the $subtract operator In this example, we are going to subtract the 250 from the "start_date" field using the $subtract operator. In MongoDB, you can use the 5 aggregation pipeline operator to subtract numbers and/or dates.Specifically, 5 can do the following three things:
The 5 operator accepts exactly two arguments. Passing the wrong number of arguments results in an error. MongoDB provides different types of arithmetic expression operators that are used in the aggregation pipeline stages and an $subtract operator is one of them. This operator is used to subtract two numbers and return the difference in the numbers or to subtract two dates and return the difference in the milliseconds, or subtracts date and number in milliseconds and returns the date. Syntax: { $subtract: [ <expression1>, <expression2> ] } Here, the given arguments must be a valid expression like numbers or a date, and the second argument is subtracted from the first argument. If you are subtracting a number from a date, then the first argument of this operator is a date. Examples: In the following examples, we are working with:
Using $subtract operator subtract two numbers: In this example, we are going to subtract 5*24*60*60000 milliseconds (i.e., 5 days) from the value of projectEndDate field using a $subtract operator. 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 expression has the following syntax: copy { $subtract: [ <expression1>, <expression2> ] } The second argument is subtracted from the first argument. The arguments can be any valid 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 . BehaviorStarting in MongoDB 4.2.12, the result will have the same type as the input except when it cannot be represented accurately in that type. In these cases:
ExamplesConsider a 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") } ])1 collection with the following documents: copy 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 NumbersThe following aggregation uses the expression to compute the 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") } ])3 by subtracting the 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") } ])4 from the subtotal of 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") } ])5 and 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") } ])6. copy db.sales.aggregate( [ { $project: { item: 1, total: { $subtract: [ { $add: [ "$price", "$fee" ] }, "$discount" ] } } } ] ) The operation returns the following results: copy { "_id" : 1, "item" : "abc", "total" : 7 } { "_id" : 2, "item" : "jkl", "total" : 19 } Subtract Two DatesThe following aggregation uses the expression to subtract 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") } ])8 from the current date, using the system (available starting in 4.2) and returns the difference in milliseconds: copy db.sales.aggregate( [ { $project: { item: 1, dateDifference: { $subtract: [ "$$NOW", "$date" ] } } } ] ) Alternatively, you can use the for the current date:s copy db.sales.aggregate( [ { $project: { item: 1, dateDifference: { $subtract: [ new Date(), "$date" ] } } } ] ) Both operations return documents that resemble the following: copy { "_id" : 1, "item" : "abc", "dateDifference" : NumberLong("186136746187") } { "_id" : 2, "item" : "jkl", "dateDifference" : NumberLong("186133146187") } Subtract Milliseconds from a DateThe following aggregation uses the expression to subtract 5 * 60 * 1000 milliseconds (5 minutes) from the “$date” field: copy db.sales.aggregate( [ { $project: { item: 1, dateDifference: { $subtract: [ "$date", 5 * 60 * 1000 ] } } } ] ) The operation returns the following results: copy { "_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) → © MongoDB, Inc 2008-present. MongoDB, Mongo, and the leaf logo are registered trademarks of MongoDB, Inc. |