# MongoDB \$subtract Operator

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:

Database: GeeksforGeeks

Collection: Employee

Document: four documents that contain the details of the employees in the form of field-value pairs. Using \$subract operator subtract two numbers:

In this example, we are going to subtract the value of firstSalary field from the value of secondSalary field using \$subtract operator.

```db.Employee.aggregate([{\$match: {department: "Development"}},
... {\$project: {result:
... {\$subtract:["\$secondSalary", "\$firstSalary"]}}}])``` Using \$subract operator subtract two dates:

In this example, we are going to subtract two dates, i.e, the value of projectStartDate field from the value of projectEndDate field using a \$subtract operator.

```db.Employee.aggregate([{\$match: {department: "Testing"}},
... {\$project:{diffResult:
... {\$subtract:["\$projectEndDate", "\$projectStartDate"]}}}])``` Using \$subract operator subtracts milliseconds from a date:

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.

```db.Employee.aggregate([{\$match: {department: "Testing"}},
... {\$project: {newprojectEndDate:
... {\$subtract:["\$projectEndDate", 5*24*60*60000]}}}])``` My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
{"ft_lang_label":"__label__en","ft_lang_prob":0.70257735,"math_prob":0.6741823,"size":2849,"snap":"2020-34-2020-40","text_gpt3_token_len":661,"char_repetition_ratio":0.20773287,"word_repetition_ratio":0.09975062,"special_character_ratio":0.23657423,"punctuation_ratio":0.17991632,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99511117,"pos_list":[0,1,2,3,4,5,6,7,8,9,10],"im_url_duplicate_count":[null,8,null,8,null,8,null,8,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-08-04T17:31:04Z\",\"WARC-Record-ID\":\"<urn:uuid:9300e0f2-f627-428c-9b1a-42446a7461c4>\",\"Content-Length\":\"100510\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:779a5365-b16c-4ab2-915f-caf980d511a3>\",\"WARC-Concurrent-To\":\"<urn:uuid:6fd2068d-604c-4fa7-811e-e0a4c14b6360>\",\"WARC-IP-Address\":\"23.199.63.80\",\"WARC-Target-URI\":\"https://www.geeksforgeeks.org/mongodb-subtract-operator/\",\"WARC-Payload-Digest\":\"sha1:RI7AZFJVHSUIPT3X2H6TEGPJLN2JPQCR\",\"WARC-Block-Digest\":\"sha1:D6AKNYQMINA4CSIRFLDOZWM55BPXRFEW\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-34/CC-MAIN-2020-34_segments_1596439735881.90_warc_CC-MAIN-20200804161521-20200804191521-00408.warc.gz\"}"}